Asesha George February 2016

echo array value return form facebook api php

i am trying to display array values return from facebook api. array is stored in $result below is my array

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [name] => Ravi Shankar
                    [id] => 10206576743272319
                )

        )

    [paging] => Array
        (
            [next] => https://graph.facebook.com/v2.5/1705944052976370/friends?limit=25&offset=25&__after_id=enc_AdBV9ZBlJwwdjBL8iWeIAZBSxDqJO0gvQWS45qwBBg1X8tCbZAoj9Cz506ZCGuDnddOL07MZD
        )

    [summary] => Array
        (
            [total_count] => 4628
        )

)

i tried using foreach but confused

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

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

i want to display the result like below:

    name    id
    xxxx   123456
    yyyy   173453

Answers


Praveen Kumar February 2016

You need to use foreach in the $result["data"]:

foreach ($result["data"] as $friend) {
  echo "{$friend["id"]} => {$friend["name"]}";
}

The each $friend variable is an array, of this format:

Array
(
    [name] => Ravi Shankar
    [id] => 10206576743272319
)


Ravi Hirani February 2016

Write your foreach loop as below:-

foreach($result['data'] as $key=>$value){
        echo $value['name'].'-'.$value['id']."<br>";           
}

Hope it will help you :)

Post Status

Asked in February 2016
Viewed 3,323 times
Voted 9
Answered 2 times

Search




Leave an answer