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

WBOY
Lepaskan: 2016-06-20 12:58:04
asal
1278 orang telah melayarinya

        $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;        }
Salin selepas log masuk

这是查询的语句
[{"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"}]
Salin selepas log masuk

查询出来的结果。 如何把这三个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"}]
Salin selepas log masuk

保存成这样的。


回复讨论(解决方案)

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

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


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

谢谢。

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan