PHP An array of problems, anxious to solve the big God, kneeling to solve, I hope that in the morning there will be a great God to help solve, thank you

Source: Internet
Author: User
One of the existing arrays is this

$arr = Array (' 0 ' = = Array (' 0 ' =>array (' price ' = ' = ' 118080814c7565909 '), ' Count ' = ' + ', ' money ' = ' $ ', ' 1 ' =>array (' price ') = ' 118080814c7eca590 ', ' count ' = ' ten ', ' money ' = ' 27 ', ' 2 ' =>array (' price ' = ' 118080814c7eca5901 ', ' count ' = ' + ', ' money ' =& Gt ' $ ',), ' 1 ' =>array (' 0 ' =>array (' price ' = ' 118080814c7565 ')                979 ', ' count ' = ' + ', ' money ' = ' $ ', ', ' 1 ' =>array (            ' Price ' = ' 118080814c7eca888 ', ' count ' = ' 101 ', ' money ' = ' 27 ',                ), ' 2 ' =>array (' price ' = ' 118080814c7eca59000 ', ' count ' = ' 160 ', ' Money ' =&GT ' $ ',), ' 2 ' =>array (' 0 ' =>array (' price ' = ' 118080814c7565 ')                009 ', ' count ' = ' "", ' money ' = ' + ', ', ' 1 ' =>array (            ' Price ' = ' 118080814c7eca670 ', ' count ' = ' + ', ' money ' = ' 217 ',                ), ' 2 ' =>array (' price ' = ' 118080814c7eca5098 ', ' count ' = ' 120 ', ' Money ' = ' 205 ',),));

What I need is to calculate the sum of the numbers in the decimal group inside the three-dimensional array, the three-dimensional inside of which there are three arrays, which requires the sum of the array count in the three second-dimension, and I foreach out and then + = Is the three-dimensional array inside all the count is added, I now do not need to ask for all and, need to separate and, to help in the following to solve the way, thank you! I don't know if I have a clear statement, if not clearly stated, thank you.

Reply content:

One of the existing arrays is this

$arr = Array (' 0 ' = = Array (' 0 ' =>array (' price ' = ' = ' 118080814c7565909 '), ' Count ' = ' + ', ' money ' = ' $ ', ' 1 ' =>array (' price ') = ' 118080814c7eca590 ', ' count ' = ' ten ', ' money ' = ' 27 ', ' 2 ' =>array (' price ' = ' 118080814c7eca5901 ', ' count ' = ' + ', ' money ' =& Gt ' $ ',), ' 1 ' =>array (' 0 ' =>array (' price ' = ' 118080814c7565 ')                979 ', ' count ' = ' + ', ' money ' = ' $ ', ', ' 1 ' =>array (            ' Price ' = ' 118080814c7eca888 ', ' count ' = ' 101 ', ' money ' = ' 27 ',                ), ' 2 ' =>array (' price ' = ' 118080814c7eca59000 ', ' count ' = ' 160 ', ' Money ' =&GT ' $ ',), ' 2 ' =>array (' 0 ' =>array (' price ' = ' 118080814c7565 ')                009 ', ' count ' = ' "", ' money ' = ' + ', ', ' 1 ' =>array (            ' Price ' = ' 118080814c7eca670 ', ' count ' = ' + ', ' money ' = ' 217 ',                ), ' 2 ' =>array (' price ' = ' 118080814c7eca5098 ', ' count ' = ' 120 ', ' Money ' = ' 205 ',),));

What I need is to calculate the sum of the numbers in the decimal group inside the three-dimensional array, the three-dimensional inside of which there are three arrays, which requires the sum of the array count in the three second-dimension, and I foreach out and then + = Is the three-dimensional array inside all the count is added, I now do not need to ask for all and, need to separate and, to help in the following to solve the way, thank you! I don't know if I have a clear statement, if not clearly stated, thank you.

$arr = Array (' 0 ' = = Array (' 0 ' =>array (' price ' = ' = ' 118080814c7565909 '), ' Count ' = ' + ', ' money ' = ' $ ', ' 1 ' =>array (' price ') = ' 118080814c7eca590 ', ' count ' = ' ten ', ' money ' = ' 27 ', ' 2 ' =>array (' price ' = ' 118080814c7eca5901 ', ' count ' = ' + ', ' money ' =& Gt ' $ ',), ' 1 ' =>array (' 0 ' =>array (' price ' = ' 118080814c7565 ')                979 ', ' count ' = ' + ', ' money ' = ' $ ', ', ' 1 ' =>array (            ' Price ' = ' 118080814c7eca888 ', ' count ' = ' 101 ', ' money ' = ' 27 ',                ), ' 2 ' =>array (' price ' = ' 118080814c7eca59000 ', ' count ' = ' 160 ', ' Money ' =&GT ' $ ',), ' 2 ' =>array (' 0 ' =>array (' price ' = ' 118080814c7565 ')                009 ', ' count ' = ' "", ' money ' = ' + ', ', ' 1 ' =>array (            ' Price ' = ' 118080814c7eca670 ', ' count ' = ' + ', ' money ' = ' 217 ',                ), ' 2 ' =>array (' price ' = ' 118080814c7eca5098 ', ' count ' = ' 120 ',    ' Money ' = ' 205 ',),); foreach ($arr as $key = = $val) {$count _arr[$key]=0;        if (Is_array ($val)) {foreach ($val as $key 1=> $val 1) {$count _arr[$key]+= $val 1[' count ']; }}}echo "
";var_dump($count_arr);exit;

The foreach perimeter defines an empty array
Then two of the foreach
In the second layer of foreach, you can count the totals in the second-level array.
And it's grouped.
The sum of each group corresponds to the elements of this array
Want to calculate all sums
And iterate over this array, right?
Phone code Word Inconvenient on the code I this method is to remove the demo data also on four lines ...
-------Split Line------
Computer on the line, put the code it

 //定义空数组 准备储存下面的数据$save=array();foreach ($arr as $key=>$list) {    //手机上想的时候忘记考虑初始化的问题    $save[$key]=0;    //遍历内部数组    foreach($list as $item){        //将内部数组的count项累加        $save[$key]+=$item['count'];    }}//打印出来var_dump($save);//上面的代码已经实现了你想要的功能 好人做到底 下面是计算全部总数的$countAll=0;foreach($save as $item){    $countAll+=$item;}var_dump($countAll);

function sum(array $array){    $tmp = 0;    array_walk($array,function($value) use(&$tmp){        isset($value['count']) && $tmp+=$value['count'];    });    return $tmp;}$sumList = array();array_walk($arr,function($value) use(&$sumList){    $sumList[] = sum($value);});var_dump($sumList);

$result =array ();
foreach ($arr as $val)
{

$count=0;foreach ($val as $v)  {      $count+=count($v);  }$result[]=$count;

}
$result This array contains the lengths of all the decimal groups in each of the two-dimensional arrays in this three-dimensional array.

  • 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.