Convert multidimensional objects to array

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I'm using amazon product advertising api. Values are returned as a multidimensional objects.

It looks like this:

object(AmazonProduct_Result)#222 (5) {   ["_code":protected]=>   int(200)   ["_data":protected]=>   string(16538)  array(2) {     ["IsValid"]=>     string(4) "True"     ["Items"]=>     array(1) {       [0]=>       object(AmazonProduct_Item)#19 (1) {         ["_values":protected]=>         array(11) {           ["ASIN"]=>           string(10) "B005HNF01O"           ["ParentASIN"]=>           string(10) "B008RKEIZ8"           ["DetailPageURL"]=>           string(120) "http://rads.stackoverflow.com/amzn/click/B005HNF01O"           ["ItemLinks"]=>           array(7) {             [0]=>             object(AmazonProduct_ItemLink)#18 (1) {               ["_values":protected]=>               array(2) {                 ["Description"]=>                 string(17) "Technical Details"                 ["URL"]=>                 string(217) "http://rads.stackoverflow.com/amzn/click/B005HNF01O"               }             }             [1]=>             object(AmazonProduct_ItemLink)#17 (1) {               ["_values":protected]=>               array(2) { 

I mean it also has array inside objects. I would like to convert all of them into a multidimensional array.

回答1:

You can use recursive function like below:

function objToArray($obj, &$arr){      if(!is_object($obj) && !is_array($obj)){         $arr = $obj;         return $arr;     }      foreach ($obj as $key => $value)     {         if (!empty($value))         {             $arr[$key] = array();             objToArray($value, $arr[$key]);         }         else         {             $arr[$key] = $value;         }     }     return $arr; } 


回答2:

I know this is old but you could try the following piece of code:

$array = json_decode(json_encode($object), true);

where $object is the response of the API.



回答3:

function convertObjectToArray($data) {     if (is_object($data)) {         $data = get_object_vars($data);     }      if (is_array($data)) {         return array_map(__FUNCTION__, $data);     }      return $data; } 

Credit to Kevin Op den Kamp.



回答4:

Just in case you came here as I did and didn't find the right answer for your situation, this modified version of one of the previous answers is what ended up working for me:

protected function objToArray($obj) {     // Not an object or array     if (!is_object($obj) && !is_array($obj)) {         return $obj;     }      // Parse array     foreach ($obj as $key => $value) {         $arr[$key] = $this->objToArray($value);     }      // Return parsed array     return $arr; } 

The original value is a JSON string. The method call looks like this:

$array = $this->objToArray(json_decode($json, true)); 


回答5:

I wrote a function that does the job, and also converts all json strings to arrays too. This works pretty fine for me.

function is_json($string) {     // php 5.3 or newer needed;     json_decode($string);     return (json_last_error() == JSON_ERROR_NONE); }  function objectToArray($objectOrArray) {     // if is_json -> decode :     if (is_string($objectOrArray)  &&  is_json($objectOrArray)) $objectOrArray = json_decode($objectOrArray);      // if object -> convert to array :     if (is_object($objectOrArray)) $objectOrArray = (array) $objectOrArray;      // if not array -> just return it (probably string or number) :     if (!is_array($objectOrArray)) return $objectOrArray;      // if empty array -> return [] :     if (count($objectOrArray) == 0) return [];      // repeat tasks for each item :     $output = [];     foreach ($objectOrArray as $key => $o_a) {         $output[$key] = objectToArray($o_a);     }     return $output; } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!