user1701252 user1701252 - 2 months ago 6
JSON Question

Implode a multidimensional JSON array (PHP) without loops

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:

[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.

I'm running PHP 5.4 so I array_column is not an option.

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"

Any help would be greatly appreciated.

Answer

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);
Comments