循环遍历所有数组时,只获取最后一个数组数据。

huangapple go评论50阅读模式
英文:

Getting only the last array data when i looped all arrays

问题

我正在尝试使用Facebook Graph API获取我的帐户的最后2篇帖子的评论。我编写了代码,它可以工作,但只返回最后一个帖子(最后的数组)的数据。

这是我的代码:

$postItems = $postsArray['posts']['data'];
foreach ($postItems as $post) {
    $fullPostId = $post['id'];
    $comments_number = 10;
    $facebook_graph_api2 = "https://graph.facebook.com/v2.4/$fullPostId/comments?access_token=$accToken&pretty=1&summary=true&limit=$comments_number&after";
    $jsonData = grab_page($facebook_graph_api2, $accCookie);
    $postDataArrays = json_decode($jsonData, true);
    $CommentsItems = $postDataArrays['data'];
}

上面的代码仅返回最后一个帖子的数据,而不是所有帖子的数据。

以下是应该返回的$postDataArrays数据的示例:

Array
(
    [data] => Array (
            [0] => Array
                (
                    [created_time] => 2023-02-08T14:46:12+0000
                    [from] => Array
                        (
                            [name] => Name1
                            [id] => id1
                        )

                    [message] => test Message
                    [id] => 61xxxxxxxxxxx_12xxxxxxxxxxxx
                )

            [1] => Array
                (
                    [created_time] => 2023-02-08T14:42:52+0000
                    [from] => Array
                        (
                            [name] => name2
                            [id] => id2
                        )

                    [message] => test Message
                    [id] => 61xxxxxxxxxx_7xxxxxxxxxxxx
                )

        )

    [paging] => Array
        (
            [next] => https://graph.facebook.com/v2.4/92xxxxxxxxxxxx_61xxxxxxxxxxxxxxxxxx/comments?access_token=EAAG&pretty=1&summary=true&limit=2&after
        )

    [summary] => Array
        (
            [order] => ranked
            [total_count] => 58
            [can_comment] => 1
        )
)
Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [created_time] => 2023-02-07T18:23:40+0000
                    [from] => Array
                        (
                            [name] => name1 from array 2
                            [id] => id2 from array 2
                        )

                    [message] => test Message
                    [id] => 6xxxxxxxxxxxxxx_xxxxxxxxxxxxxxxx
                )

            [1] => Array
                (
                    [created_time] => 2023-02-07T18:20:49+0000
                    [from] => Array
                        (
                            [name] => name2 from array 2
                            [id] => id2 from array 2
                        )

                    [message] => test Message
                    [id] => 6xxxxxxxxxxxxxx_xxxxxxxxxx
                )

        )

    [paging] => Array
        (
            [next] => https://graph.facebook.com/v2.4/9xxxxxxxxxxxxxxxxxxxxx/comments?access_token=EAAG&pretty=1&summary=true&limit=2&after
        )

    [summary] => Array
        (
            [order] => ranked
            [total_count] => 121
            [can_comment] => 1
        )
)

上面的数组应该返回4个用户,但它只返回最后2个用户的数据。

英文:

I'm trying to use Facebook Graph API to get comments from the last 2 posts from my accounts
I wrote the code and it works but it only returns the second post (the last array) only

this my code :

$postItems = $postsArray['posts']['data'];
foreach ($postItems as $post) {
$fullPostId = $post['id'];
$comments_number = 10;
$facebook_graph_api2 = "https://graph.facebook.com/v2.4/$fullPostId/comments?access_token=$accToken&pretty=1&summary=true&limit=$comments_number&after";
$jsonData = grab_page($facebook_graph_api2,$accCookie);
$postDataArrays = json_decode($jsonData, true);
$CommentsItems = $postDataArrays['data'];
}
//foreach($CommentItems as $item) {
//  echo $users = $item['from']['id'].'\n';
//}
// @$postItems = $postsArray['data'];
/** Update Balance Code **/
$toolId = '87637';
@$itemsCount = @count($CommentItems);
$toolPrice = toolPrice($toolId)*$itemsCount;
$finalBalance = updateBalance($toolPrice);
/** Update Balance Code **/
if ($finalBalance) {
if (isset($CommentItems)) {
echo '{"status":1,"balance":'.$finalBalance.',"message":"Success !","data":"';
foreach($CommentItems as $item) {
echo $users = $item['from']['id'].'\n';
// echo  json_encode($users, JSON_FORCE_OBJECT);
// $items[] = $item['from']['id'];
}
// print_r($items);
// echo '{"status":1,"message":"Success !","data":"'.$items.'"}';
echo '"}';
}elseif( isset($postDataArray['error']) && $postDataArray['error']['code']== 1 ){
$msgError = $postDataArray['error']['message'];
echo '{"status":0,"message":"Error !","reason":"'.$msgError.'"}';
}else{
echo '{"status":0,"message":"Error !","reason":"Please Update Your Facebook Access Token"}';
}
}else{
echo '{"status":0,"message":"Error !","reason":"You do not have enough funds on balance"}';
}

The code above returns only last array data not all posts data.
this is an example of $postDataArrays data should be

Array
(
[data] => Array (
[0] => Array
(
[created_time] => 2023-02-08T14:46:12+0000
[from] => Array
(
[name] => Name1
[id] => id1
)
[message] => test Message
[id] => 61xxxxxxxxxxx_12xxxxxxxxxxxx
)
[1] => Array
(
[created_time] => 2023-02-08T14:42:52+0000
[from] => Array
(
[name] => name2
[id] => id2
)
[message] => test Message
[id] => 61xxxxxxxxxx_7xxxxxxxxxxxx
)
)
[paging] => Array
(
[next] => https://graph.facebook.com/v2.4/92xxxxxxxxxxxx_61xxxxxxxxxxxxxxxxxx/comments?access_token=EAAG&pretty=1&summary=true&limit=2&after
)
[summary] => Array
(
[order] => ranked
[total_count] => 58
[can_comment] => 1
)
)
Array
(
[data] => Array
(
[0] => Array
(
[created_time] => 2023-02-07T18:23:40+0000
[from] => Array
(
[name] => name1 from array 2
[id] => id2 from array 2
)
[message] => test Message
[id] => 6xxxxxxxxxxxxxx_xxxxxxxxxxxxxxxx
)
[1] => Array
(
[created_time] => 2023-02-07T18:20:49+0000
[from] => Array
(
[name] => name2 from array 2
[id] => id2 from array 2
)
[message] => test Message
[id] => 6xxxxxxxxxxxxxx_xxxxxxxxxx
)
)
[paging] => Array
(
[next] => https://graph.facebook.com/v2.4/9xxxxxxxxxxxxxxxxxxxxx/comments?access_token=EAAG&pretty=1&summary=true&limit=2&after
)
[summary] => Array
(
[order] => ranked
[total_count] => 121
[can_comment] => 1
)
)

the arrays above should return 4 users but it returns only last 2 users

答案1

得分: 1

$responses= array();

foreach ($postItems as $post) {
$fullPostId = $post['id'];
$comments_number = 3000;
$facebook_graph_api2 = "https://graph.facebook.com/v2.4/$fullPostId/comments?access_token=$accToken&pretty=1&summary=true&limit=$comments_number&after";
$jsonData = grab_page($facebook_graph_api2,$accCookie);
$postDataArray = json_decode($jsonData, true);
$CommentsItems = $postDataArray['data'];
foreach($CommentsItems as $item) {
$users = $item['from']['id'];
$userId['user_id'] = $users;
$responses[] = $userId['user_id'];
}
}

$enResponse = json_encode($responses);
$allComments = json_decode($enResponse,true);

foreach($allComments as $item) {
echo $users = $item.'\n';
}

英文:

First i create an array

$responses= array();

And modified the code and put all neded data inside the array

foreach ($postItems as $post) {
$fullPostId = $post['id'];
$comments_number = 3000;
$facebook_graph_api2 = "https://graph.facebook.com/v2.4/$fullPostId/comments?access_token=$accToken&pretty=1&summary=true&limit=$comments_number&after";
$jsonData = grab_page($facebook_graph_api2,$accCookie);
$postDataArray = json_decode($jsonData, true);
$CommentsItems = $postDataArray['data'];
foreach($CommentsItems as $item) {
$users = $item['from']['id'];
$userId['user_id'] = $users;
$responses[] = $userId['user_id'];
}
}

Then , Encoded the the final response to be json , and i decoded it again

$enResponse = json_encode($responses);
$allComments = json_decode($enResponse,true);

To show all data , just loop $allComments

foreach($allComments as $item) {
echo $users = $item.'\n';
}

huangapple
  • 本文由 发表于 2023年2月8日 23:32:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定