>  기사  >  백엔드 개발  >  如何将多个json组合成一个?

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

WBOY
WBOY원래의
2016-06-20 12:58:041278검색

        $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으로 문의하세요.