将多维对象转换为数组

时间:2012-11-26 15:29:09

标签: php arrays multidimensional-array

我正在使用亚马逊产品广告API。值作为多维对象返回。

看起来像这样:

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://www.amazon.com/Case-Logic-TBC-302-FFP-Compact/dp/B005HNF01O?SubscriptionId=AKIAJNFRQCIJLTY6LDTA&tag=*********-20"
          ["ItemLinks"]=>
          array(7) {
            [0]=>
            object(AmazonProduct_ItemLink)#18 (1) {
              ["_values":protected]=>
              array(2) {
                ["Description"]=>
                string(17) "Technical Details"
                ["URL"]=>
                string(217) "http://www.amazon.com/Case-Logic-TBC-302-FFP-Compact/dp/tech-data/B005HNF01O%3FSubscriptionId%3DAKIAJNFRQCIJLTY6LDTA%26tag%*******-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB005HNF01O"
              }
            }
            [1]=>
            object(AmazonProduct_ItemLink)#17 (1) {
              ["_values":protected]=>
              array(2) {

我的意思是它也有对象内的数组。我想将它们全部转换为多维数组。

6 个答案:

答案 0 :(得分:101)

我知道这已经过时但您可以尝试以下代码:

  

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

其中$ object是API的响应。

答案 1 :(得分:18)

您可以使用如下的递归函数:

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 :(得分:2)

/wp-json/wp/v2/posts/<post_id>

感谢Kevin Op den Kamp。

答案 3 :(得分:1)

这是一个古老的问题,但是我最近遇到了这个问题,并提出了自己的解决方案。

array_walk_recursive($array, function(&$item){
    if(is_object($item)) $item = (array)$item;
});

现在,如果$array本身就是对象,则可以将其转换为数组,然后再将其放入array_walk_recursive

$array = (array)$object;
array_walk_recursive($array, function(&$item){
    if(is_object($item)) $item = (array)$item;
});

迷你示例:

array_walk_recursive($array,function(&$item){if(is_object($item))$item=(array)$item;});

在我的情况下,我有一个来自第三方资源的stdClass对象数组,该字段具有一个字段/属性,我需要使用该字段的值作为参考来找到包含stdClass的值,以便可以访问该元素中的其他数据。基本上比较2个数据集中的嵌套键。

我必须做很多次,所以我不想为每个需要查找的项目进行详细介绍。解决该问题的方法通常是array_column,但这不适用于对象。所以我首先做了上面的事情。

干杯!

答案 4 :(得分:0)

万一你像我一样来到这里,并没有找到适合你情况的正确答案,之前答案之一的修改版本最终为我工作:

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;
}

原始值是JSON字符串。方法调用如下所示:

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

答案 5 :(得分:0)

我写了一个完成这项工作的函数,并且还将所有json字符串转换为数组。这对我来说非常好。

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;
}
相关问题