Shubham M
Shubham M

Reputation: 69

How to get the books liked by facebook user from the array

I have used the Facebook PHP SDK to fetch the books user liked.

$books = $facebook->api('/me/books');

After decoding the JSON I get the output array something like this:

Array ( 
[data] => Array ( 
[0] => Array ( [category] => Book [name] => Steve Jobs by Walter Isaacson [id] => 152707268157952 [created_time] => 2012-12-05T15:10:14+0000 ) 
[1] => Array ( [category] => Book [name] => Restful Web Services [id] => 106832016007551 [created_time] => 2012-12-10T10:26:45+0000 ) 
[2] => Array ( [category] => Book [name] => DOS for Dummies [id] => 112172905468817 [created_time] => 2012-12-05T15:10:14+0000 )
....
....
....
)

Now what I want to do is to separate the [name] and create a new array for the book name. What should be the loop for doing this?

EDIT: First I am checking if the array is not empty, not sure what to do next. I am trying something like this:

foreach($books['data'] as $booksliked){
    if(!empty($booksliked['data'])) {
        foreach($booksliked['data']['name'] as $blikesData){
        //Not sure what to put here 
        }
    }
}

Upvotes: 0

Views: 179

Answers (1)

Wing Lian
Wing Lian

Reputation: 2418

The below should work

<?php
$books_from_fb = $facebook->api('/me/books');
$books = array();
foreach ($books_from_fb['data'] as $book_arr) {
    $books[] = $book_arr['name'];
}

Upvotes: 2

Related Questions