首頁  >  文章  >  後端開發  >  如何将多个json组合成一个?

如何将多个json组合成一个?

WBOY
WBOY原創
2016-06-20 12:58:041270瀏覽

        $question = $_GET['question'];        $arr = explode("|",$question);        for($i = 0;$i <= 3; $i++){            $rid = $arr[$i];            $sql = "select * from xs_radio where rid = '$rid'";            $result = mysql_query($sql);    		$arr = array();    		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){    			$ar[] = $row;      		}    		$js = json_encode($ar);            echo $js;        }

这是查询的语句
[{"rid":"1","title":"\u6211\u662f\u8c01","a":"\u5b8b\u5411\u4e1c","b":"\u9b4f\u4eae","c":"\u738b\u514b\u5a07","d":"\u5b8b\u5b50\u6587","answer":"\u5b8b\u5411\u4e1c"}][{"rid":"2","title":"\u6211\u662f\u8c01","a":"\u9b4f\u4eae","b":"\u5218\u5fb7\u534e","c":"\u674e\u5a1c","d":"\u5f20\u626c","answer":"\u9b4f\u4eae"}][{"rid":"3","title":"\u6211\u662f\u8c01","a":"\u9b4f\u4eae","b":"\u738b\u514b\u5a07","c":"\u674e\u56fd\u4f1f","d":"\u738b\u83f2","answer":"\u738b\u514b\u5a07"}][{"rid":"16","title":"\u6211\u662f\u8c01","a":"\u5218\u5f66\u5bbe","b":"\u674e\u56fd\u632f","c":"\u7a0b\u79c0\u82b9","d":"\u738b\u514b\u5a07","answer":"\u738b\u514b\u5a07"}]

查询出来的结果。 如何把这三个json组合成一个?
[{"rid":"1","title":"\u6211\u662f\u8c01","a":"\u5b8b\u5411\u4e1c","b":"\u9b4f\u4eae","c":"\u738b\u514b\u5a07","d":"\u5b8b\u5b50\u6587","answer":"\u5b8b\u5411\u4e1c"},{"rid":"2","title":"\u6211\u662f\u8c01","a":"\u9b4f\u4eae","b":"\u5218\u5fb7\u534e","c":"\u674e\u5a1c","d":"\u5f20\u626c","answer":"\u9b4f\u4eae"},{"rid":"3","title":"\u6211\u662f\u8c01","a":"\u9b4f\u4eae","b":"\u738b\u514b\u5a07","c":"\u674e\u56fd\u4f1f","d":"\u738b\u83f2","answer":"\u738b\u514b\u5a07"},{"rid":"16","title":"\u6211\u662f\u8c01","a":"\u5218\u5f66\u5bbe","b":"\u674e\u56fd\u632f","c":"\u7a0b\u79c0\u82b9","d":"\u738b\u514b\u5a07","answer":"\u738b\u514b\u5a07"}]

保存成这样的。


回复讨论(解决方案)

转数组 然后合成一个数组 再转json

把 $ar[] = $row; 
改为 $ar[$i][] = $row; 


$js = json_encode($ar);
echo $js;
移到 for 循环后面

谢谢。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn