ホームページ  >  記事  >  バックエンド開発  >  複数のJSONを1つに結合する方法

複数のJSONを1つに結合する方法

WBOY
WBOYオリジナル
2016-06-13 12:18:232784ブラウズ

複数のjsonを1つに結合するにはどうすればよいですか?

この投稿の最終編集者は sigkl00181 2015-04-22 14:15:39
<br />        $question = $_GET['question'];<br />        $arr = explode("|",$question);<br />        for($i = 0;$i <= 3; $i++){<br />            $rid = $arr[$i];<br />            $sql = "select * from xs_radio where rid = '$rid'";<br />            $result = mysql_query($sql);<br />    		$arr = array();<br />    		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){<br />    			$ar[] = $row;  <br />    		}<br />    		$js = json_encode($ar);<br />            echo $js;<br />        }<br />

これはクエリ ステートメントの結果です
<br />[{"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"}]<br />[{"rid":"2","title":"\u6211\u662f\u8c01","a":"\u9b4f\u4eae","b":"\u5218\u5fb7\u534e","c":"\u674e\u5a1c","d":"\u5f20\u626c","answer":"\u9b4f\u4eae"}]<br />[{"rid":"3","title":"\u6211\u662f\u8c01","a":"\u9b4f\u4eae","b":"\u738b\u514b\u5a07","c":"\u674e\u56fd\u4f1f","d":"\u738b\u83f2","answer":"\u738b\u514b\u5a07"}]<br />[{"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"}]<br />

。これら 3 つの JSON を 1 つに結合するにはどうすればよいでしょうか?
<br />[{"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"},<br />{"rid":"2","title":"\u6211\u662f\u8c01","a":"\u9b4f\u4eae","b":"\u5218\u5fb7\u534e","c":"\u674e\u5a1c","d":"\u5f20\u626c","answer":"\u9b4f\u4eae"},<br />{"rid":"3","title":"\u6211\u662f\u8c01","a":"\u9b4f\u4eae","b":"\u738b\u514b\u5a07","c":"\u674e\u56fd\u4f1f","d":"\u738b\u83f2","answer":"\u738b\u514b\u5a07"},<br />{"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"}]<br />

このように保存します。
------解決策----------------------
配列を変換し、それを組み合わせて配列を変換し、json
-----解決策のアイデア----------------------
Put $ar [] = $ row;
は $ar[$i][] = $row;


$js = json_encode($ar);
echo $js;
から for ループ後
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。