Deepak Singh
Deepak Singh

Reputation: 35

how to echo array data from pinterest oauth api php

I am trying to get user profile from pinterest using oauth api. code for user data :

$me = $pinterest->users->me(array(
'fields' => 'username,first_name,last_name,image[large]'
));

and echo result by :

echo $me;

the output is as follow :

{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}

Now i want to echo this result as

id="195414208739840616"
username="rajivsharma033"
first_name="Rajiv"

and so on... please help me.

Upvotes: 1

Views: 224

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

Since you are getting json data so you need to use json_decode():-

   <?php
    $me = '{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}';

    $array_data = json_decode($me); 
    echo "<pre/>";print_r($array_data);

    foreach ($array_data as $key=>$value){

        if($key == 'image'){
            echo $key. " url is=" . $value->large->url .'<br/>';
        }else{

            echo $key. "=" . $value .'<br/>';
        }
    }

Upvotes: 1

A. Fink
A. Fink

Reputation: 171

I would solve it by using two times foreach:

$a = json_decode($me);
 foreach ($a as $key=>$value){
echo $key.'="'.$value.'"<br/>';
}
foreach ($a['image']['large'] as $key=>value){
 echo 'image-large-'$key.'="'.$value.'"<br/>';
}

Alternatively, you can do this recursive:

function echojson($string=''){
 $a = json_decode($me);
 foreach ($a as $key=>$value){
  if (is_array($value)) echojson($string.'-'.$key);
  else
  echo $string.$key.'="'.$value.'"<br/>';
 }
}

Upvotes: 0

Related Questions