内置一个没有循环的多维JSON数组(PHP)

时间:2022-02-15 22:09:14

I'm working with a REST API and the response is given back in JSON. Here's a print_r version of what it looks like:

我正在使用REST API,响应以JSON的形式返回。这是一个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.

任何帮助将不胜感激。

1 个解决方案

#1


2  

This is simply a problem with how you are accessing the property - it's an object, not an array:

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

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

#1


2  

This is simply a problem with how you are accessing the property - it's an object, not an array:

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

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