How do I convert an object to an array?

后端 未结 11 1395
别那么骄傲
别那么骄傲 2020-11-22 10:36
response->docs);
?>

Outputs the following:

    Array 
(
    [0] => Object 
            (         


        
11条回答
  •  执念已碎
    2020-11-22 11:14

    //My Function is worked. Hope help full for you :)
          $input = [
                '1' => (object) [1,2,3],
                '2' => (object) [4,5,6,
                    (object) [6,7,8,
                    [9, 10, 11,
                        (object) [12, 13, 14]]]
                ],
                '3' =>[15, 16, (object)[17, 18]]
            ];
    
            echo "
    ";
            var_dump($input);
            var_dump(toAnArray($input));
    
          public function toAnArray(&$input) {
    
            if (is_object($input)) {
                $input = get_object_vars($input);
            }
            foreach ($input as &$item) {
                if (is_object($item) || is_array($item)) {
                    if (is_object($item)) {
                        $item = get_object_vars($item);
                    }
                    self::toAnArray($item);
                }
            }
        }
    

提交回复
热议问题