gpt4 book ai didi

php - 使用 PHP 循环后将数组附加到 JSON 输出

转载 作者:行者123 更新时间:2023-11-29 12:45:00 25 4
gpt4 key购买 nike

本质上,数据应如下所示,但作为包含其信息的每个用户的一个数组。完全循环后应将其打包为 JSON。

前言

使用带有 print_r 的 pre 标签,我使用一些幽默的示例数据得到了它应该看起来的样子:

User 1 is following:Array(    [0] => Array        (            [first] => Hugh            [last] => Janus            [bg] => /resources/bg/2/b.png            [prof_pic] => /resources/profilepics/2/b.jpg            [bio] => 50% proctologist, 50% baker.        ))Array(    [0] => Array        (            [first] => Andrew            [last] => Dickens            [bg] => /resources/bg/3.c.jpg            [prof_pic] => /resources/profilepics/3/c.jpeg            [bio] => Therapists are pretty cool.        ))Array(    [0] => Array        (            [first] => Anita            [last] => Dickson            [bg] => /resources/bg/4/d.jpg            [prof_pic] => /resources/profilepics/4/d.JPG            [bio] => Just another barista.        ))

Essentially, everything needs to come back as one JSON item that someone could use for any purpose with the supplied info.

Background on the Following Code

This code selects all User IDs from the table 'followers' where the person who is following someone is equal to fB and the person who owns that friendship is fA. In this instance, userID 1 is trying to look at a page of every person they follow with their profile pic, background pic, first name, last name, and bio. The data that the JSON is outputting will be formatted in another file, but this is just being used to curate the raw data.

$dbh is the PDO database connection and the code uses prepared statements for security.

Code

$flingJSON = 0;
$userid = "1";
$result = $dbh->prepare('SELECT fA
FROM followers
WHERE fB=:userid');
$result->bindParam(':userid', $userid);
$result->execute();
$rawData = $result->fetchAll(PDO::FETCH_ASSOC);
foreach($rawData as $following) {
$followingID = $following['fA'];
$getInfoFling = $dbh->prepare('SELECT first, last, bg, prof_pic, bio
FROM users
WHERE id=:flingID
');
$getInfoFling->bindParam(':flingID', $followingID);
$getInfoFling->execute();
$flingJSON = $getInfoFling->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($flingJSON);
echo "</pre>";
}

提前非常感谢您的帮助,作为感谢,您将在该文件的文档中得到感谢。

最佳答案

这应该返回一个包含所有用户信息的数组:

//Create empty data array
$flingData = array();
$userid = "1";
$result = $dbh->prepare('SELECT fA
FROM followers
WHERE fB=:userid');
$result->bindParam(':userid', $userid);
$result->execute();
$rawData = $result->fetchAll(PDO::FETCH_ASSOC);
foreach($rawData as $following) {
$followingID = $following['fA'];
$getInfoFling = $dbh->prepare('SELECT first, last, bg, prof_pic, bio
FROM users
WHERE id=:flingID
');
$getInfoFling->bindParam(':flingID', $followingID);
$getInfoFling->execute();
//Append user info to data array
$flingData[] = $getInfoFling->fetch(PDO::FETCH_ASSOC);
}

//Create JSON string
$flingJSON = json_encode($flingData);

echo "<pre>";
print_r($flingJSON);
echo "</pre>";

关于php - 使用 PHP 循环后将数组附加到 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25577404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com