使用mysql_fetch_assoc的JSON多维数组

时间:2016-02-05 06:21:09

标签: php mysql arrays json mysqli

我的Mysqli更新了查询并输出了

SELECT milestone.id, milestone.name, milestone.date, milestone.location, milestone.story_body, milestone.short_link, milestone.created_at, 
GROUP_CONCAT(images.path) as path , images.update_type
FROM milestone
INNER JOIN images ON milestone.id = images.update_id
WHERE milestone.business_id =  '1' && milestone.status =  '1' && images.update_type =  '3'
GROUP BY milestone.id

DB Capture

如何使用上述查询形成JSON对象?

我已尝试过以下方法,但没有给出任何结果

$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array[] = $row;

}

我想要这样的东西 -

[  
       {  
          "id":"4",
          "name":"2nd anniversary",
          "date":"2015-12-17",
          "location":"Mumbai",
          "story_body":"Gzjjs jdk djks jdks jdkd jx djdb djd JD djbd djdj d",
          "short_link":"izWfs",
          "created_at":"2015-12-11 03:49:52",
          "path":
                 [                       
                   {"\/SupportData\/ImpalzB2B\/uploads\/90294930451448437444826.jpg"},
                   {"\/SupportData\/ImpalzB2B\/uploads\/90294930451449758248579.jpg"}
                 ],
          "update_type":"3"
       },       
       {  
          "id":"7",
          "name":"#1styearAnniversary",
          "date":"2016-01-20",
          "location":"Mumbai",
          "story_body":"Bsjsj jdkdk djdkdk dkdkf kdkf dkfj fjfj fjfkjdd djkd",
          "short_link":"FHXh0",
          "created_at":"2016-01-20 23:10:54",
          "path":"\/SupportData\/ImpalzB2B\/uploads\/11453356652175.jpg",
          "update_type":"3"
       }
    ] 

注意:我知道PHP 7中没有使用Mysql。我需要将其替换为PDO& Mysqli所以请忽略那个错误。我正在努力同时我正面临这个问题。

1 个答案:

答案 0 :(得分:0)

你现在可以这样做 -

$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $temp= explode(',', $row['path']); // explode by ,
    if(count($temp) > 1) { // More than one element then assign
        $row['path']= $temp;
    } 
    $result_array[] = $row;
}