Home > Backend Development > PHP Tutorial > 求foreach输出数组判断输出代码,部分已经完成

求foreach输出数组判断输出代码,部分已经完成

WBOY
Release: 2016-06-20 12:29:19
Original
960 people have browsed it

有数组

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		
Copy after login




ps: [CounterCode] => 0003 (字符串)
[Countername] => 南楼二层(字符串)
[BuildDate] => 20130105 (日期)
[xsje] => 30111 (金额)
想要输出的格式为(含义:CounterCode=0002 销售金额按日输出)
CounterCode: '0002',data: [97600,84808,112221.8,100316,48966]},CounterCode: '0003',data: [54862,61934,47470,39975,30111]},
Copy after login



现在我能输出
{name:'0002',data:[]},{name:'0003',data:[]},{name:'0004',data:[]},{name:'0005',data:[]},{name:'0006',data:[]},{name:'0008',data:[]},{name:'0009',data:[]}
Copy after login


代码
<?foreach ( $users as $user ) { echo ",{name:'".$user->CounterCode."',"; echo "data:[" ;求这部分代码echo "]}"; }?>
Copy after login



另求删除最后一个标点符号方法


回复讨论(解决方案)

$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]}]
Copy after login

谢谢 jam00

刚没注意,,不是我要求的格式啊?

CounterCode: '0002',data: [97600,84808,112221.8,100316,48966]},
CounterCode: '0003',data: [54862,61934,47470,39975,30111]},
我要的上这样的

foreach($tmpxsje as $k=>$v){
    $tmp['CounterCode '] = $k;
    $tmp['data'] = $v;
    $data[] = $tmp;
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template