如何从数组中获取facebook用户喜欢的书籍

时间:2012-12-11 06:12:37

标签: php arrays facebook facebook-php-sdk

我使用Facebook PHP SDK来获取用户喜欢的书籍。

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

解码JSON后,我得到如下输出数组:

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 )
....
....
....
)

现在我要做的是分离[name]并为书名创建一个新数组。这样做的循环应该是什么?

修改 首先,我正在检查数组是否为空,不知道下一步该做什么。我正在尝试这样的事情:

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

1 个答案:

答案 0 :(得分:2)

以下应该有效

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