配列があります
Array( [0] => stdClass Object ( [CounterCode] => 0002 [Countername] => 南楼一层 [BuildDate] => 20130101 [xsje] => 97600 ) [1] => stdClass Object ( [CounterCode] => 0002 [Countername] => 南楼一层 [BuildDate] => 20130102 [xsje] => 84808 ) [2] => stdClass Object ( [CounterCode] => 0002 [Countername] => 南楼一层 [BuildDate] => 20130103 [xsje] => 112221.8 ) [3] => stdClass Object ( [CounterCode] => 0002 [Countername] => 南楼一层 [BuildDate] => 20130104 [xsje] => 100316 ) [4] => stdClass Object ( [CounterCode] => 0002 [Countername] => 南楼一层 [BuildDate] => 20130105 [xsje] => 48966 ) [5] => stdClass Object ( [CounterCode] => 0003 [Countername] => 南楼二层 [BuildDate] => 20130101 [xsje] => 54862 ) [6] => stdClass Object ( [CounterCode] => 0003 [Countername] => 南楼二层 [BuildDate] => 20130102 [xsje] => 61934 ) [7] => stdClass Object ( [CounterCode] => 0003 [Countername] => 南楼二层 [BuildDate] => 20130103 [xsje] => 47470 ) [8] => stdClass Object ( [CounterCode] => 0003 [Countername] => 南楼二层 [BuildDate] => 20130104 [xsje] => 39975 ) [9] => stdClass Object ( [CounterCode] => 0003 [Countername] => 南楼二层 [BuildDate] => 20130105 [xsje] => 30111 ) 。。。N
CounterCode: '0002',data: [97600,84808,112221.8,100316,48966]},CounterCode: '0003',data: [54862,61934,47470,39975,30111]},
{name:'0002',data:[]},{name:'0003',data:[]},{name:'0004',data:[]},{name:'0005',data:[]},{name:'0006',data:[]},{name:'0008',data:[]},{name:'0009',data:[]}
<?foreach ( $users as $user ) { echo ",{name:'".$user->CounterCode."',"; echo "data:[" ;求这部分代码echo "]}"; }?>
$tmpxsje = [];foreach($users as $user){ $tmpxsje[$user->CounterCode][] = $user->xsje;}$data = [];foreach($tmpxsje as $k=>$v){ $tmp['name'] = $k; $tmp['data'] = $v; $data[] = $tmp;}echo json_encode($data);//[{"name":"0002","data":[97600,84808,112221.8,100316,48966]},{"name":"0003","data":[54862,61934,47470,39975,30111]}]
ありがとう jam00
気付かなかったんですが、要求した形式ではありませんか?
カウンターコード: '0002'、データ: [97600,84808,112221.8,100316,48966]}、
カウンターコード: '0003'、データ: [54862,61934,47470,39975,30111]} ,
こんな感じです
foreach($tmpxsje as $k=>$v){
$tmp['CounterCode '] = $k; tmp['data'] = $v; $data[] = $tmp;