在PHP中构建嵌套数组

时间:2014-03-02 14:18:21

标签: php mysql json

所以我正在尝试使用嵌套在主用户数组中的数组构建一个JSON对象。

我在PHP中构建数据模型时遇到了一些麻烦。任何人都可以提供任何指示吗?

期望的输出:

[{
            id: 2,
            name: "John Wilson",
            profilePic: "fighter-1.jpg",
            activities: [{
                id: 6431,
                time: (57).minutes().ago(),
                points: 20
            }, {
                id: 6431,
                time: new Date(),
                points: 20
            }]
        }, {
            id: 3,
            name: "Christoper Robin",
            profilePic: "fighter-3.jpg",
            activities: [{
                id: 6431,
                time: (1).days().ago,
                points: 20
            }, {
                id: 6431,
                time: (2).days().ago,
                points: 20
            }, {
                id: 6431,
                time: new Date(),
                points: 20
            }]
        }, {
            id: 1,
            name: "Jerry Seinfeld",
            profilePic: "placeholder.jpg",
            activities: [{
                id: 6431,
                time: new Date(),
                points: 20
            }, {
                id: 6431,
                time: new Date(),
                points: 20
            }, {
                id: 6432,
                time: new Date(),
                points: 100
            }]
        }]

PHP和MySQL:

$getCompUsers = mysql_query("SELECT c.userid, u.name, u.profilePic, a.activity_typeid, a.userid, a.time, a.activity_weight, a.activityname
        FROM competitionmembers c 
        INNER JOIN users1 u ON  u.id = c.userid 
        INNER JOIN activity_entries a ON a.userid = u.id
        WHERE c.competitionid = '$competitionId'") or die("Couldn't select competitions users");

$compUsersArr = array();
while ($row = mysql_fetch_array($getCompUsers,MYSQL_ASSOC)){

$compUsersArr = array(
            'id' => $row["userid"],
            'name'=> $row["name"],
            'profilePic' => $row["profilePic"], 
                $activities = array( //THIS SEEMS TO BE THE PROBLEM
                    'id' => $row["activity_typeid"],
                    'name' => $row["activityname"],
                    'points' => $row["activity_weight"]
                    )
            );
    }

2 个答案:

答案 0 :(得分:2)

你制作数组的方式是错误的,嵌套arrays你不必像在数组中一样使用变量

$compUsersArr = array(
        'id' => $row["userid"],
        'name'=> $row["name"],
        'profilePic' => $row["profilePic"], 
        'activities' => array(
            'id' => $row["activity_typeid"],
            'name' => $row["activityname"],
            'points' => $row["activity_weight"]
        )
);

答案 1 :(得分:1)

实际上,你的代码几乎是正确的;拆分它会让你明白它为什么没有:

$activities = array(
    'id' => $row["activity_typeid"],
    'name' => $row["activityname"],
    'points' => $row["activity_weight"]
);

$compUsersArr = array(
    'id' => $row["userid"],
    'name'=> $row["name"],
    'profilePic' => $row["profilePic"], 
    $activities,
);

这将创建一个类似的数组:

[
    'id' => 123, 
    'name' => 'foo', 
    'profilePic' => 'bar', 
    0 => ['id' => 456, 'name' => 'baz', 'points' => 'quz']
]

你可能想要这个:

$compUsersArr = array(
    'id' => $row["userid"],
    'name'=> $row["name"],
    'profilePic' => $row["profilePic"], 
    'activities' => $activities,
);