首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

json数据格式拼凑,数组合并,

2012-02-19 
json数据格式拼凑,数组合并,求助!有三个数组$arr array(张三,李四,王五)$arr2 array(100,200,3

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…

[解决办法]
<?php
$arr = array('张三','李四','王五');
$arr2 = array(100,200,300);
$arr3 = array(50,60,70);
$date=time();
$result['total']=sizeof($arr);
for($i=0;$i<sizeof($arr);$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"}
]
}
*/

?>

热点排行