Multiple arrays form another array-php Tutorial

Source: Internet
Author: User
The following array is formed in the program when multiple arrays form another array (the keys in the program are dynamic and are written to an end here)
Array(    [Category] => TOTAL    [January2014_Number_stores] => 0    [February2014_Number_stores] => 0    [March2014_Number_stores] => 0)

There are arrays
Array(    [January2014_Number_stores] => 155    [February2014_Number_stores] => 155    [March2014_Number_stores] => 155)Array(    [January2014_Number_stores] => 126    [February2014_Number_stores] => 126    [March2014_Number_stores] => 126)Array(    [January2014_Number_stores] => 37    [February2014_Number_stores] => 37    [March2014_Number_stores] => 37)Array(    [January2014_Number_stores] => 9    [February2014_Number_stores] => 9    [March2014_Number_stores] => 9)Array(    [January2014_Number_stores] => 4    [February2014_Number_stores] => 4    [March2014_Number_stores] => 4)

Now we need to assign the sum of the same key values in the array in the second group to the first array? If the key value is dynamic and regular, how should I write it?


Reply to discussion (solution)

$a = array(  'Category' => 'TOTAL',  'January2014_Number_stores' => 0,  'February2014_Number_stores' => 0,  'March2014_Number_stores' => 0,);$b = array(  array(    'January2014_Number_stores' => 155,    'February2014_Number_stores' => 155,    'March2014_Number_stores' => 155,  ),  array(    'January2014_Number_stores' => 126,    'February2014_Number_stores' => 126,    'March2014_Number_stores' => 126,  ),  array(    'January2014_Number_stores' => 37,    'February2014_Number_stores' => 37,    'March2014_Number_stores' => 37,  ),  array(    'January2014_Number_stores' => 9,    'February2014_Number_stores' => 9,    'March2014_Number_stores' => 9,  ),  array(    'January2014_Number_stores' => 4,    'February2014_Number_stores' => 4,    'March2014_Number_stores' => 4,  ),);foreach($b as $r) {  foreach($r as $k=>$v) {    if(! isset($a[$k])) $a[$k] = 0;    $a[$k] += $v;  }}print_r($a);
Array
(
[Category] => TOTAL
[January2014_Number_stores] = & gt; 331
[February2014_Number_stores] = & gt; 331
[March2014_Number_stores] = & gt; 331
)

$a = array(  'Category' => 'TOTAL',  'January2014_Number_stores' => 0,  'February2014_Number_stores' => 0,  'March2014_Number_stores' => 0,);$b = array(  array(    'January2014_Number_stores' => 155,    'February2014_Number_stores' => 155,    'March2014_Number_stores' => 155,  ),  array(    'January2014_Number_stores' => 126,    'February2014_Number_stores' => 126,    'March2014_Number_stores' => 126,  ),  array(    'January2014_Number_stores' => 37,    'February2014_Number_stores' => 37,    'March2014_Number_stores' => 37,  ),  array(    'January2014_Number_stores' => 9,    'February2014_Number_stores' => 9,    'March2014_Number_stores' => 9,  ),  array(    'January2014_Number_stores' => 4,    'February2014_Number_stores' => 4,    'March2014_Number_stores' => 4,  ),);foreach($b as $r) {  foreach($r as $k=>$v) {    if(! isset($a[$k])) $a[$k] = 0;    $a[$k] += $v;  }}print_r($a);
Array
(
[Category] => TOTAL
[January2014_Number_stores] = & gt; 331
[February2014_Number_stores] = & gt; 331
[March2014_Number_stores] = & gt; 331
)
If you want to set the format of the data in the summation array (one for every three bits), where should the number_format function be written?

Format after calculation

$ar = array(  'Category' => 'TOTAL',  'January2014_Number_stores' => 10331,  'February2014_Number_stores' => 500331,  'March2014_Number_stores' => 4512331,);foreach($ar as &$v) if(is_numeric($v)) $v = number_format($v);print_r($ar);
Array
(
[Category] => TOTAL
[January2014_Number_stores] = & gt; 10,331
[February2014_Number_stores] = & gt; 500,331
[March2014_Number_stores] = & gt; 4,512,331
)

Format after calculation

$ar = array(  'Category' => 'TOTAL',  'January2014_Number_stores' => 10331,  'February2014_Number_stores' => 500331,  'March2014_Number_stores' => 4512331,);foreach($ar as &$v) if(is_numeric($v)) $v = number_format($v);print_r($ar);
Array
(
[Category] => TOTAL
[January2014_Number_stores] = & gt; 10,331
[February2014_Number_stores] = & gt; 500,331
[March2014_Number_stores] = & gt; 4,512,331
)
If I want to assign the first two items to the third item (three data items in one group), how can I write them?
$ Ctt = 0; foreach ($ items_sum as & $ v) {if ($ ctt % 3 = 0) {$ v = ....; // The first two items are mutually exclusive to get the third item} if (is_numeric ($ v) $ v = number_format ($ v); $ ctt ++ ;}

This operation should generally be performed using the association key.

This operation should generally be performed using the association key.
Can you give me more details...

$ Ar = array (
'Category' => 'total ',
'January2014 _ Number_stores '=> 10331,
'February2014 _ Number_stores '=> 500331,
'March2014 _ Number_stores '=> 4512331,
);

$ Ar ['march2014 _ Number_stores '] = $ ar ['january2014 _ Number_stores']/$ ar ['february2014 _ Number_stores '];

In actual use, check whether the divisor ($ ar ['february2014 _ Number_stores ']) is equal to 0.


Format after calculation

$ar = array(  'Category' => 'TOTAL',  'January2014_Number_stores' => 10331,  'February2014_Number_stores' => 500331,  'March2014_Number_stores' => 4512331,);foreach($ar as &$v) if(is_numeric($v)) $v = number_format($v);print_r($ar);
Array
(
[Category] => TOTAL
[January2014_Number_stores] = & gt; 10,331
[February2014_Number_stores] = & gt; 500,331
[March2014_Number_stores] = & gt; 4,512,331
)
If I want to assign the first two items to the third item (three data items in one group), how can I write them?
$ Ctt = 0; foreach ($ items_sum as & $ v) {if ($ ctt % 3 = 0) {$ v = ....; // The first two items are mutually exclusive to get the third item} if (is_numeric ($ v) $ v = number_format ($ v); $ ctt ++ ;}

$ctt=0;$s1=0;$s2=0;foreach($items_sum as &$v) {if($ctt==1){$s1=$v;}if($ctt==2){$s2=$v;}if($ctt%3==0){$v =xx($s2,$s1);$ctt=0;}if(is_numeric($v)) $v = number_format($v);$ctt++;}
Related Article

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.