Evaluate the foreach output array to determine the output code, partially completed

Source: Internet
Author: User
Evaluate the foreach output array to determine the output code. some of the output code has been completed with arrays.
Array ([0] => stdClass Object ([CounterCode] => 0002 [Countername] => South floor [BuildDate] => 20130101 [xsje] => 97600) [1] => stdClass Object ([CounterCode] => 0002 [Countername] => South floor [BuildDate] => 20130102 [xsje] => 84808) [2] => stdClass Object ([CounterCode] => 0002 [Countername] => South floor [BuildDate] => 20130103 [xsje] => 112221.8) [3] = & gt; stdClass Object ([CounterCode] = & gt; 0002 [Counternam E] => [BuildDate] => 20130104 [xsje] => 100316) [4] => stdClass Object ([CounterCode] => 0002 [Countername] => South floor [BuildDate] => 20130105 [xsje] => 48966) [5] => stdClass Object ([CounterCode] => 0003 [Countername] => South Building 2 [BuildDate] => 20130101 [xsje] => 54862) [6] => stdClass Object ([CounterCode] => 0003 [Countername] => South Building 2 [BuildDate] => 20130102 [xsje] => 61934) [7] => stdcia Ss Object ([CounterCode] => 0003 [Countername] => South building second floor [BuildDate] => 20130103 [xsje] => 47470) [8] => stdClass Object ([CounterCode] => 0003 [Countername] => South Building 2 [BuildDate] => 20130104 [xsje] => 39975) [9] => stdClass Object ([CounterCode] => 0003 [Countername] => South Building 2 [BuildDate] => 20130105 [xsje] => 30111 )... N




Ps: [CounterCode] = & gt; 0003 (string)
[Countername] => second floor, South Building (string)
[BuildDate] = & gt; 20130105 (date)
[Xsje] => 30111 (amount)
The format to be output is (meaning: CounterCode = 0002 sales amount is output by day)
CounterCode: '0002',data: [97600,84808,112221.8,100316,48966]},CounterCode: '0003',data: [54862,61934,47470,39975,30111]},



Now I can output
{name:'0002',data:[]},{name:'0003',data:[]},{name:'0004',data:[]},{name:'0005',data:[]},{name:'0006',data:[]},{name:'0008',data:[]},{name:'0009',data:[]}


Code
 CounterCode. "',"; echo "data: ["; find this part of code echo "]}" ;}?>



Delete the last punctuation mark


Reply to discussion (solution)

$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]}]

Thank you, jam00.

I just did not pay attention to it. isn't it the format I requested?

CounterCode: '20180101', data: [97600,84808, 0002, 112221.8, 48966]},
CounterCode: '000000', data: [54862,61934, 47470,39975, 0003]},
I want something like this.

Foreach ($ tmpxsje as $ k => $ v ){
$ Tmp ['countercode'] = $ k;
$ Tmp ['data'] = $ v;
$ Data [] = $ tmp;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.