本文介绍了如何访问已被改造成一个对象数组的属性/值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何可以访问哪些已被改造成一个对象的属性/数组的价值呢?举例来说,我想在索引0来访问值,
$ OBJ =(对象)阵列('qualitypoint','技术','印度');
的var_dump($ obj-大于0);
错误
解决方案
The reason you can not access values via $obj->0
its because it against PHP variable naming see http://php.net/manual/en/language.variables.basics.php for more information. even if you use ArrayObject
you would still have the same issues
but there is a patch to this ... you can convert all integer keys to string or write your own conversion function
Example
$array = array('qualitypoint', 'technologies', 'India' , array("hello","world"));
$obj = (object) $array;
$obj2 = arrayObject($array);
function arrayObject($array)
{
$object = new stdClass();
foreach($array as $key => $value)
{
$key = (string) $key ;
$object->$key = is_array($value) ? arrayObject($value) : $value ;
}
return $object ;
}
var_dump($obj2->{0}); // Sample Output
var_dump($obj,$obj2); // Full Output to see the difference
$sumObject = $obj2->{3} ; /// Get Sub Object
var_dump($sumObject->{1}); // Output world
Output
string 'qualitypoint' (length=12)
Full output
object(stdClass)[1]
string 'qualitypoint' (length=12)
string 'technologies' (length=12)
string 'India' (length=5)
array
0 => string 'hello' (length=5)
1 => string 'world' (length=5)
object(stdClass)[2]
public '0' => string 'qualitypoint' (length=12)
public '1' => string 'technologies' (length=12)
public '2' => string 'India' (length=5)
public '3' =>
object(stdClass)[3]
public '0' => string 'hello' (length=5)
public '1' => string 'world' (length=5)
Multi Array Outpur
Thanks
:)
这篇关于如何访问已被改造成一个对象数组的属性/值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!