php 访问对象的属性(当属性的键名称为数字时)的方法 {}
这篇文章下面写了一个 将 数组转化成对象的方法, 这个数组的键为数字(即数组为索引数组), 访问对象的时候, $obj->1 显然错位,那到底该怎么样访问对象的数字属性呢?
$obj->{1} ; 表示的是访问对象 $obj 的属性的键为 1 对应的属性值!
<?php /** * Created by PhpStorm. * User: FLY.YUAN * Date: 16-6-14 * Time: 下午2:26 */ function toObject($array) { //$o = (object)''; $o = new \stdClass(); foreach ($array as $key => $value) { if (is_array($value)) { $value = toObject($value); } $o->$key = $value; } return $o; } $obj = toObject(array( array(1=>23))); echo "<pre class="brush:php;toolbar:false">"; //JSON encode, and then decode your object passing true as the second param in the decode function. //This will return an associative array. //var_dump(json_decode(json_encode($obj), true)); var_dump($obj->{0}->{1}); $a = array('123' => '123', '123foo' => '123foo'); $o = json_decode(json_encode($a), true); var_dump($o); function recursive_cast_to_array($o) { $a = (array)$o; foreach ($a as &$value) { if (is_object($value)) { $value = recursive_cast_to_array($value); } } return $a; } $myVar = toObject(array('highlighting' => array('448364' => array('Data' => array(123))))); $arr = recursive_cast_to_array($myVar); echo 134; var_dump( $myVar->highlighting->{448364}->Data->{0}); var_dump(NULL == false); $test = array(); $test['$.router'] = 123; var_dump($test);
以上是php 如何访问对象的属性的详细内容。更多信息请关注PHP中文网其他相关文章!