How to convert an array to object in PHP?

前端 未结 30 2844
说谎
说谎 2020-11-22 02:48

How can I convert an array like this to an object?

[128] => Array
    (
        [status] => "Figure A.
 Facebook\'s horizontal scrollbars showing u         


        
30条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 03:05

    You could also use an ArrayObject, for example:

    1,"two"=>2,"three"=>3), 
                     array("one"=>1,"two"=>2,"three"=>3)
               );
        $o = new ArrayObject($arr);
        echo $o->offsetGet(2)["two"],"\n";
        foreach ($o as $key=>$val){
            if (is_array($val)) {
                foreach($val as $k => $v) {
                   echo $k . ' => ' . $v,"\n";
                }
            }
            else
            {
                   echo $val,"\n";
            }
        }
    ?>
    
    //Output:
      2
      test
      one => 1
      two => 2
      three => 3
      one => 1
      two => 2
      three => 3
    

提交回复
热议问题