<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
  <head>
   <title> objectToArray2.php </title>
   <meta charset="UTF-8">
   <meta name="Author" content="">
   <meta name="Keywords" content="">
   <meta name="Description" content="">
  </head> <body>
 <?php
 class Test{
     public $a;
     public $b;
     public function __construct($a) {
         $this->a = $a;
     }
 }$test = new Test('test1');
 $test->b = new Test('test2');print_r($test);//Test Object ( [a] => test1 [b] => Test Object ( [a] => test2 [b] => ) ) 
 echo '<hr>';
 echo '<hr>';
 //
 $arr = get_object_vars($test);
 var_dump($arr);
 echo '<hr>';
 echo '<hr>';
 //
 //对象转数组,使用get_object_vars返回对象属性组成的数组function objectToArray($obj){
     $arr = is_object($obj) ? get_object_vars($obj) : $obj;
     if(is_array($arr)){
         return array_map(__FUNCTION__, $arr);
     }else{
         return $arr;
     }
 } $array = objectToArray($test);
 print_r($array);//Array ( [a] => test1 [b] => Array ( [a] => test2 [b] => ) ) 
 echo '<hr>';
 if(isset($array['b']['b']))
 {
  echo $array['b']['b'];
  echo '<hr>';
 }
 else
 {
  echo "not set array['b']['b'] <hr>";
 }
 //exit(0) or die('bye...');
 //
 echo '<hr>';echo '<hr>';echo '<hr>';
 //数组转对象
 function arrayToObject($arr){
     if(is_array($arr)){
         return (object) array_map(__FUNCTION__, $arr);
     }else{
         return $arr;
     }
 }$object = arrayToObject($array);
 print_r($object);
 echo '<br>';?>
 </body>
 </html>
Test Object ( [a] => test1 [b] => Test Object ( [a] => test2 [b] => ) ) 
array
   'a' => string 'test1' (length=5)
   'b' => 
     object(Test)[2]
       public 'a' => string 'test2' (length=5)
       public 'b' => null
Array ( [a] => test1 [b] => Array ( [a] => test2 [b] => ) ) 
 not set array['b']['b'] 
stdClass Object ( [a] => test1 [b] => stdClass Object ( [a] => test2 [b] => ) )