php 对象数组互转

数组转对象 function array2object($array) {   if (is_array($array)) {     $obj = new StdClass();     foreach ($array as $key => $val){       $obj->$key = $val;     }   }   else { $obj = $array; }   return $obj; }     $array = array('foo' => 'bar','one' => 'two','three' => 'four'); $obj = array2object($array); print $obj->one; // output's "two"   对象专属组 function object2array($object) {   if (is_object($object)) {     foreach ($object as $key => $value) {       $array[$key] = $value;     }   }   else {     $array = $object;   }   return $array; }  

$obj = Object
(
[foo] => bar
[one] => two
[three] => four
)

$arr = object2array($obj); print $arr['foo']; // output's bar
上一篇:two pointers思想 ---- 利用两个i, j两个下标,同时对序列进行扫描,以O(n)复杂度解决问题的一种思想


下一篇:刷题Two