0

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.

user1701252
  • 1,501
  • 1
  • 11
  • 19
  • [ramsey/array_column](https://github.com/ramsey/array_column) provides `array_column()` support in PHP versions < 5.5. – Gerard Roche Sep 27 '16 at 00:12
  • Thanks. I just installed it, but I think the problem is that I'm trying to implode from an object. So array_map or array_column won't work. – user1701252 Sep 27 '16 at 00:22

2 Answers2

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);
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • I just tried this and I'm getting this message: Warning: implode(): Invalid arguments passed – user1701252 Sep 26 '16 at 23:39
  • @user1701252 if you post an example online somewhere, e.g. https://eval.in then I can help - otherwise I'd be guessing, but it's likely something silly you're missing, like a wrong variable name – scrowler Sep 26 '16 at 23:40
  • shouldn't that be `$topics_array` inside argument #2, anyway, the OP should have made that correction to fit the current env – Kevin Sep 26 '16 at 23:46
  • @RobbieAverill I don't think either array_map or array_column will work since it is within an object. I ran a test as a multidimensional array, and it works fine. I may just have to rework my code to make this work unless you have an alternate method on imploding multidimensional arrays within objects? – user1701252 Sep 27 '16 at 00:25
  • @user1701252 the example data you posted is an array of objects. If your actual data is not the same as your example then please update your question with accurate example data – scrowler Sep 27 '16 at 03:04
  • 1
    Problem solved. I did not set my json_decode($content,true). I left out the true leaving it as an object. Thanks for your help. – user1701252 Sep 27 '16 at 04:13
0

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. 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);
rüff0
  • 906
  • 1
  • 12
  • 26