0

我使用 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 
        }
    }
}
4

1 回答 1

2

下面应该工作

<?php
$books_from_fb = $facebook->api('/me/books');
$books = array();
foreach ($books_from_fb['data'] as $book_arr) {
    $books[] = $book_arr['name'];
}
于 2012-12-11T06:18:34.467 回答