简体   繁体   English

无需循环即可转换多维JSON数组(PHP)

[英]Implode a multidimensional JSON array (PHP) without loops

I'm working with a REST API and the response is given back in JSON. 我正在使用REST API,响应以JSON返回。 Here's a print_r version of what it looks like: 这是其外观的print_r版本:

        [topics] => Array
            (
                [0] => stdClass Object
                    (
                        [provider] => klout
                        [value] => Facebook
                    )

                [1] => stdClass Object
                    (
                        [provider] => klout
                        [value] => Business
                    )
                [2] => stdClass Object
                    (
                        [provider] => klout
                        [value] => LinkedIn
                    )

I need to take the [provider] value and convert it into a string. 我需要采用[provider]值并将其转换为字符串。

I'm running PHP 5.4 so I array_column is not an option. 我正在运行PHP 5.4,所以我不能选择array_column。

I've tried the instructions from here: 我已经尝试过这里的指示:

$topics_array = $json->digitalFootprint->topics;
$arr = array_map(function($topics_array){ return $topics_array['value']; }, $arr);
$implode_topics = implode("[,,]", $arr);

But am returned with an error mentioning "Cannot use object of type stdClass as array" 但是返回了一个错误消息,提示“无法将stdClass类型的对象用作数组”

Any help would be greatly appreciated. 任何帮助将不胜感激。

这只是访问属性的方式的问题-它是一个对象,而不是数组:

$arr = array_map(function($topics_array){ return $topics_array->value; }, $arr);

try using array_map with null function in a nested foreach cycles, then colecting the values on a temporal array, and finally imploding this array. 尝试在嵌套的foreach周期中使用具有null函数的array_map,然后在时间数组上选取值,最后内嵌该数组。 ex: 例如:

$arr = array_map(null,$topics_array);
$arr_temp = [];
foreach(array_map(null,$arr) as $i) {
    foreach(array_map(null,$i) as $j) {
      $arr_temp[] = ($j->value);
    }
}

$result_string = implode(', ', $arr_temp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM