LoginSignup
8
9

More than 5 years have passed since last update.

オブジェクトを配列にキャストする

Posted at

オブジェクトにオブジェクト内オブジェクトが...といりこ状になっているオブジェクトの場合は、再帰的に (array) をかける必要があります。便宜的に次のような関数を定義すると楽。

PHP
function obj2arr($obj)  
{  
    if ( !is_object($obj) ) return $obj;  

    $arr = (array) $obj;  

    foreach ( $arr as &$a )  
    {  
        $a = obj2arr($a);  
    }  

    return $arr;  
}  

引用:スイナシア

8
9
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9