how can I retrieve specific data from an array

Hi all, I’m starting to use php for integrate my app with facebook, anyway,

I have this array:

( [data] => Array ( [0] => stdClass Object ( [name] => Lisa [id] => 1383095935338077 ) [1] => stdClass Object ( [name] => Karen [id] => 1375898729393793 ) ) [paging] => stdClass Object ( [next] => https://graph.facebook.com/v2.2/1378922702423461/friends?access_token=CAAsTF4JGDmCO6iq ) [summary] => stdClass Object ( [total_count] => 2 ) )

and I would like to retrive a data of the user like:

Lisa|1383095935338077|Karen|1375898729393793…

I know I have to make a loop, and I have this script:

[php]

$result = $me->asArray();

// Get user’s friends
$friends = $result[‘data’];

foreach ($friends as $key => $value) {
echo $friends[$key] = (array)$value; //this retrieve just “Array”
echo “|”;
//echo $friends[0][‘id’]; //this retrieve the id of Lisa, but repeat two times…
}
[/php]

someone can help me please?

thank you very much

Well, not tested, but, try this way:
[php]
$result = $me->asArray();

// Get user’s friends
$friends = $result[‘data’];

foreach ($friends as $friend) {
echo $friend[‘name’] . “|” . $friend[‘id’];
}
[/php]
The array is associated to [number][name][id], so you have to pull it out that way.
If you break up the $friends into $key and $value, then you have to pull it out without the index.
Something like echo $value; But, that could cause issues as it would dump all the values…

Let us know if that works for you…

You have an array of objects, you could just use them as is. I’m guessing $me also contains an object, no need to convert that either.

This should replace everything you pasted

[php]foreach ($me->data as $friend) {
echo $friend->name . ’ |’ . $friend->id . ‘|’;
}[/php]

If not then I might have been wrong about $me

[php]$result = $me->asArray();

foreach ($result[‘data’] as $friend) {
echo $friend->name . ’ |’ . $friend->id . ‘|’;
}[/php]

Note how we use [‘key’] to access array keys, and ->property to access properties of an object.

Thanks JimL as I forgot to add in the ending .’|’; …

Ah, didnt even notice :stuck_out_tongue:

Thank you very much for the reply!!!

thank you very much!!! it’s work!!! I try just last one, and is working!! thank you very much! I spent days to find some solution…I see a lot of example but never works for me… I’m not very good with php!!

I’m curios about the difference of the last two…

If I understand, $friends is an array right? for each friends (x,y repeat? = the height of the array? (2))

$result = $me->asArray(); this convert the result in array?

thank you!! really, :smiley:

Yes, friends is an array (list) containing objects. Which are converted to a pure array containing arrays by.executing that function (toArray())

If you want a visual representation of the data you have then do this

[php]echo ‘

’;
print_r($friends);[/php]

It will pretty print the content of the variable and you can use it for simple debugging. It helps a lot in seeing how the data is structured

thank you very much :slight_smile:

Another problem ???

I’m trying to retrieve another array:

this is the array I get with: print_r ($resultScore);

Array ( [0] => stdClass Object ( [user] => stdClass Object ( [id] => 1378518079120107 [name] => Mary Amhheghcggci ) [score] => 267089 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [1] => stdClass Object ( [user] => stdClass Object ( [id] => 1374605922846447 [name] => Harrylollo Baosky ) [score] => 76102 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [2] => stdClass Object ( [user] => stdClass Object ( [id] => 1382836682021405 [name] => Luca Rosenthalstein ) [score] => 35297 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [3] => stdClass Object ( [user] => stdClass Object ( [id] => 1374550636185549 [name] => Paolo Warmanberg ) [score] => 31920 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565) ) )

and I use this script:

[php]$session = new \Facebook\FacebookSession($accessToken);

$requestScore = (new \Facebook\FacebookRequest( $session, ‘GET’, ‘/760699587345565/scores’))->execute()->getGraphObject(\Facebook\GraphUser::className());

$resultScore = $requestScore ->asArray();
$friendsScore = $resultScore[‘data’];
print_r ($resultScore);

foreach ($resultScore[‘data’] as $friendsScore) {
echo $friendsScore->name . ’ |’ . $friendsScore->id . ‘|’;
}[/php]

do you know why I get this error?

Warning: Invalid argument supplied for foreach() in /index.php on line 82  ???

I tried to change a lot of thing…but doesn’t works… I see the difference about ( [data] => Array ( and in this other array, I don’t understand why doesn’t appear… isn’t this script to do that:?

[php]$friendsScore = $resultScore[‘data’];[/php]

Thank you very much, and sorry

there is no ‘data’ key directly in the top level, so $resultscore[‘data’] doesn’t exist.

thank you for the reply, I have to change the order of [‘data’]? why in this new array, the script doesn’t works well?

The result you get doesn’t seem to contain the ‘data’ part at all, so just remove it. It seems like the previous request you did returned something like

[php][
‘status’ => ‘ok’,
‘data’ => [
‘your array with results here’
]
][/php]

while this request returns

[php][
‘your array with results here’
][/php]

ok, if I run this script:

[php]$session = new \Facebook\FacebookSession($accessToken);

$requestScore = (new \Facebook\FacebookRequest( $session, ‘GET’, ‘/760699587345565/scores’))->execute()->getGraphObject(\Facebook\GraphUser::className());

//$resultScore = $requestScore ->asArray();
//$friendsScore = $resultScore[‘data’];
print_r ($requestScore);[/php]

the print_r return this:

Facebook\GraphUser Object ( [backingData:protected] => Array ( [0] => stdClass Object ( [user] => stdClass Object ( [id] => 1378518079120107 [name] => Mary Amhheghcggci ) [score] => 267089 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [1] => stdClass Object ( [user] => stdClass Object ( [id] => 1374605922846447 [name] => Harrylollo Baosky ) [score] => 76102 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565) ) ) )

and if I run the others 2 lines:

$resultScore = $requestScore ->asArray();
$friendsScore = $resultScore[‘data’];

the Facebook\GraphUser Object ( [backingData:protected] => disappear, so, I’m missing something…this isn’t an correct array? It’s possible to fix it?

thanks :slight_smile:

I’m sadly unfamiliar with the facebook v4 api for php

if you do this, what do you get?

[php]$session = new \Facebook\FacebookSession($accessToken);

$requestScore = (new \Facebook\FacebookRequest( $session, ‘GET’, ‘/760699587345565/scores’))->execute()->getGraphObject(\Facebook\GraphUser::className());

$resultScore = $requestScore ->asArray();
//$friendsScore = $resultScore[‘data’];
echo ‘

’; // this is what actually makes the dump below pretty printed
print_r ($resultScore);[/php]

this:

Array ( [0] => stdClass Object ( [user] => stdClass Object ( [id] => 1378518079120107 [name] => Mary Amhheghcggci )
        [score] => 267089
        [application] => stdClass Object
            (
                [name] => EatSquare
                [namespace] => eatsquare
                [id] => 760699587345565
            )

    )

[1] => stdClass Object
    (
        [user] => stdClass Object
            (
                [id] => 1374605922846447
                [name] => Harrylollo Baosky
            )

        [score] => 76102
        [application] => stdClass Object
            (
                [name] => EatSquare
                [namespace] => eatsquare
                [id] => 760699587345565
            )

    )

[2] => stdClass Object
    (
        [user] => stdClass Object
            (
                [id] => 1382836682021405
                [name] => Luca Rosenthalstein
            )

        [score] => 35297
        [application] => stdClass Object
            (
                [name] => EatSquare
                [namespace] => eatsquare
                [id] => 760699587345565
            )

    )

[3] => stdClass Object
    (
        [user] => stdClass Object
            (
                [id] => 1374550636185549
                [name] => Paolo  Warmanberg
            )

        [score] => 31920
        [application] => stdClass Object
            (
                [name] => EatSquare
                [namespace] => eatsquare
                [id] => 760699587345565
            )

   )

)

in order, If I follow https://developers.facebook.com/docs/graph-api/reference/v2.2/user/scores this, and I change the script with this new one:

[php]$requestScore = new FacebookRequest( $session, ‘GET’, ‘/760699587345565/scores’);
$responseScore = $requestScore->execute();
$graphObject = $responseScore->getGraphObject();

//$requestScore = $responseScore ->asArray();
//$friendsScore = $resultScore[‘data’];
print_r ($responseScore);[/php]

I have this with print_r:

Facebook\FacebookResponse Object ( [request:Facebook\FacebookResponse:private] => Facebook\FacebookRequest Object ( [session:Facebook\FacebookRequest:private] => Facebook\FacebookSession Object ( [accessToken:Facebook\FacebookSession:private] => Facebook\Entities\AccessToken Object ( [accessToken:protected] => CAAKz2iyvPJ0BAGxs2ybaEp9kIQditnIQXZALGTSTyP1jXybxb1x56pAo6FWKqL6fDYsp4YNwpcLGUMNktaS2Fv0bt6l527ZBIjSE2dnn2SRwZC216NdoobZASle3BeuwCjajUDE1BeyUD6T9Yfs5CmeaI1fOIBgZBj0unfFgCZAaMFz7oZCycCTEd1ZBe6LH4NyuElpyfJlNthfWlZCi3McROrnL4seC3TekZD [machineId:protected] => [expiresAt:protected] => DateTime Object ( [date] => 2015-02-18 16:00:00.000000 [timezone_type] => 3 [timezone] => America/Chicago ) ) [signedRequest:Facebook\FacebookSession:private] => ) [method:Facebook\FacebookRequest:private] => GET [path:Facebook\FacebookRequest:private] => /760699587345565/scores [params:Facebook\FacebookRequest:private] => Array ( [access_token] => CAAKz2iyvPJ0BAGxs2ybaEp9BeyUD6T9Yfs5CmeaI1fOIBgZBj0unfFgCZAaMFz7oZCycCTEd1ZBe6LH4NyuElpyfJlNthfWlZCi3McROrnL4seC3TekZD [appsecret_proof] => 14d7b1e946d396c16c7779046065c332aac889254defb54403b0ef59fcba81dd ) [version:Facebook\FacebookRequest:private] => v2.2 [etag:Facebook\FacebookRequest:private] => ) [responseData:Facebook\FacebookResponse:private] => stdClass Object [b]( [data] => Array[/b] ( [0] => stdClass Object ( [user] => stdClass Object ( [id] => 1378518079120107 [name] => Mary Amhheghcggci ) [score] => 267089 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [1] => stdClass Object ( [user] => stdClass Object ( [id] => 1374605922846447 [name] => Harrylollo Baosky ) [score] => 76102 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [2] => stdClass Object ( [user] => stdClass Object ( [id] => 1382836682021405 [name] => Luca Rosenthalstein ) [score] => 35297 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [3] => stdClass Object ( [user] => stdClass Object ( [id] => 1374550636185549 [name] => Paolo Warmanberg ) [score] => 31920 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [4] => stdClass Object ( [user] => stdClass Object ( [id] => 1374474482860205 [name] => Linda Greeneescu ) [score] => 5558 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [5] => stdClass Object ( [user] => stdClass Object ( [id] => 1382608605376929 [name] => Marika Cecol ) [score] => 5071 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [6] => stdClass Object ( [user] => stdClass Object ( [id] => 1380890392215920 [name] => Bob Amhhdiagjhfg ) [score] => 4869 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [7] => stdClass Object ( [user] => stdClass Object ( [id] => 1380761488895509 [name] => Barbara Amhhejacjhda ) [score] => 4297 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [8] => stdClass Object ( [user] => stdClass Object ( [id] => 1380688315569460 [name] => Susan Amhhdiadjhgf ) [score] => 4033 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [9] => stdClass Object ( [user] => stdClass Object ( [id] => 1377665785873042 [name] => Mike Amhhhikfead ) [score] => 1237 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [10] => stdClass Object ( [user] => stdClass Object ( [id] => 1374577759516072 [name] => Giacomo Baowitz ) [score] => 520 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) [11] => stdClass Object ( [user] => stdClass Object ( [id] => 1379395622366168 [name] => Virginia Warmansky ) [score] => 366 [application] => stdClass Object ( [name] => EatSquare [namespace] => eatsquare [id] => 760699587345565 ) ) ) ) [rawResponse:Facebook\FacebookResponse:private] => {"data":[{"user":{"id":"1378518079120107","name":"Mary Amhheghcggci"},"score":267089,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1374605922846447","name":"Harrylollo Baosky"},"score":76102,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1382836682021405","name":"Luca Rosenthalstein"},"score":35297,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1374550636185549","name":"Paolo Warmanberg"},"score":31920,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1374474482860205","name":"Linda Greeneescu"},"score":5558,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1382608605376929","name":"Marika Cecol"},"score":5071,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1380890392215920","name":"Bob Amhhdiagjhfg"},"score":4869,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1380761488895509","name":"Barbara Amhhejacjhda"},"score":4297,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1380688315569460","name":"Susan Amhhdiadjhgf"},"score":4033,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1377665785873042","name":"Mike Amhhhikfead"},"score":1237,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1374577759516072","name":"Giacomo Baowitz"},"score":520,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}},{"user":{"id":"1379395622366168","name":"Virginia Warmansky"},"score":366,"application":{"name":"EatSquare","namespace":"eatsquare","id":"760699587345565"}}]} [etagHit:Facebook\FacebookResponse:private] => [etag:Facebook\FacebookResponse:private] =>

I see an array and a json format?? I edited this original script with the first one I had post… and I retrive just the Array, but in this one, I can see the [data] => Array, this is should the correct way right? but I don’t know how I can retrieve the array from this last quote… I tried the method

->asArray(); but doesn’t works…

thank you :slight_smile:

Ok. What is your desired output?

I would like to have the output like in the first array

[php] foreach ($friends as $friend) {
echo $friendsScore->name . ’ |’ . $friendsScore->id . ‘|’. $friendsScore->score. ‘|’;
}[/php]

I’m curious how is possible to extrapolate the array in the last quote and get works the foreach part :slight_smile:

thank you very much, really :slight_smile:

[php]$session = new \Facebook\FacebookSession($accessToken);

$requestScore = (new \Facebook\FacebookRequest( $session, ‘GET’, ‘/760699587345565/scores’))->execute()->getGraphObject(\Facebook\GraphUser::className());

$results = $requestScore ->asArray();
foreach ($results as $result) {
echo $result->user->name . ’ |’ . $result->user->id . ‘|’. $result->score . ‘|’;
}[/php]

thank you very much!! I see the difference :smiley:

I also unfamiliar on how to use this feature on my facebook page which is about http://naturessmile.com and tips to reverse gums recession.

Sponsor our Newsletter | Privacy Policy | Terms of Service