php json數組問題,$result1和$result2兩個數組合并成一個,單獨輸出 echo json_encode($result1); echo json_encode($result2);
最後輸出效果:
{
“id”:”111”,
“image_name”:”我的臥室“,
“image_url”:“d:/image/a.png”,
comments:[{
“uid”,“122”,
“cname“:”小明“,
“ctime”,“2014-12-12”,
}]
}
做出這種效果, echo json_encode( array($result1, $result2) );這樣輸出的不對,是直接在後面加上了,而沒有成單獨的數組
該怎麼做呢
回複討論(解決方案)
你的 $result1、$result2 都是什嗎?
單獨的數組是什麼樣的,貼出最後結果看看。
echo json_encode( array(array("content"=>$result1), $result2) ) 是這樣?
$result1 和 $result2 都?有?出。
echo json_encode(array_merge($result1, array("comments"=>$result2))) 這樣??
你的 $result1、$result2 都是什嗎?
$result 1 就是“id”:”111”,
“image_name”:”我的臥室“,
“image_url”:“d:/image/a.png”,
這些,
$result 2
單獨的數組是什麼樣的,貼出最後結果看看。
單獨的就是“id”:”111”,
“image_name”:”我的臥室“,
“image_url”:“d:/image/a.png”,
後面的是$result2
$result1 和 $result2 都?有?出。
$result1就是上半部,$result2就是下半部
$result1 = array( 'id' => 111, 'image_name' => '我的臥室', 'image_url' => 'd:/image/a.png',);$result2 = array( 'comments' => array( array( 'uid' => 122, 'cname' => '小明', 'ctime' => '2014-12-12', ), ),);echo json_encode(array_merge($result1, $result2));
$result1 = array('id' => '111','image_name' => '我的臥室','image_url' => 'd:/image/a.png');$result2 = array('comments' => array(array('uid' => '122','cname' => '小明','ctime' => '2014-12-12')));$result = array_merge($result1, $result2);echo json_encode($result);
我覺得樓主的問題似乎沒有那麼簡單,給兩個數組讓你調用函數嗎?
我看了半天問題就是沒看懂意思.
我覺得樓主的問題似乎沒有那麼簡單,給兩個數組讓你調用函數嗎?
我看了半天問題就是沒看懂意思.
就是有兩個數組,把他們輸出成一個json數組