Heim  >  Artikel  >  Backend-Entwicklung  >  如何将多个json组合成一个?

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

WBOY
WBOYOriginal
2016-06-20 12:58:041270Durchsuche

        $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 循环后面

谢谢。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP防XSS 防SQL注入的代码Nächster Artikel:求大神,smarty错误