json資料格式拼湊,數組合并,
來源:互聯網
上載者:User
json資料格式拼湊,數組合并,求助!
有三個數組
$arr = array('張三','李四','王五');
$arr2 = array(100,200,300);
$arr3 = array(50,60,70);
如何平湊成另外一個數組$array,最後echo json_encode($array)輸出如下格式
{"total":"3",
"result":[
{"date":"1","name":"張三","value":"100","other":"50"},
{"date":"1","name":"李四","value":"200","other":"60"},
{"date":"1","name":"王五","value":"300","other":"70"}
]
}
兄弟們,Help…
------解決方案--------------------
$arr = array('張三','李四','王五');
$arr2 = array(100,200,300);
$arr3 = array(50,60,70);
$date=time();
$result['total']=sizeof($arr);
for($i=0;$i{
$result['result'][]=array("date"=>$date,"name"=>$arr[$i],"value"=>$arr2[$i],"other"=>$arr3[$i]);
}
echo json_encode($result);
/*
{"total":"3",
"result":[
{"date":"1","name":"張三","value":"100","other":"50"},
{"date":"1","name":"李四","value":"200","other":"60"},
{"date":"1","name":"王五","value":"300","other":"70"}
]
}
*/
?>