首頁 > 後端開發 > php教程 > 多个数组形成另一个数组的问题

多个数组形成另一个数组的问题

WBOY
發布: 2016-06-23 13:59:44
原創
848 人瀏覽過

程序中形成了以下的数组(程序的中的key是动态的,这里写死了)

Array(    [Category] => TOTAL    [January2014_Number_stores] => 0    [February2014_Number_stores] => 0    [March2014_Number_stores] => 0)
登入後複製

又有数组
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)
登入後複製

现在要把第二组中的数组中key值相同的相加求和后赋值给第一个数组,该怎么写? 如果key值是动态有规律的该怎么写?


回复讨论(解决方案)

$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] => 331
[February2014_Number_stores] => 331
[March2014_Number_stores] => 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] => 331
[February2014_Number_stores] => 331
[March2014_Number_stores] => 331
)
如果要设置求和所得数组中数据的格式(每3位一个,),number_format函数该写在哪里?

算完了再格式化

$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] => 10,331
[February2014_Number_stores] => 500,331
[March2014_Number_stores] => 4,512,331
)

算完了再格式化

$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] => 10,331
[February2014_Number_stores] => 500,331
[March2014_Number_stores] => 4,512,331
)
再请教下,如果我要把前两项相除赋值给第三项(3个数据为一组)该怎么写?
$ctt=0;	foreach($items_sum as &$v) {		if($ctt%3==0){			$v =....;//前两项相除得到第三项		}		if(is_numeric($v)) $v = number_format($v);		$ctt++;	}
登入後複製
登入後複製

这一般应该用关联键操作

这一般应该用关联键操作
大侠,能详细点吗...

$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'];

实际使用时还需检查除数($ar['February2014_Number_stores'])是否等于 0


算完了再格式化

$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] => 10,331
[February2014_Number_stores] => 500,331
[March2014_Number_stores] => 4,512,331
)
再请教下,如果我要把前两项相除赋值给第三项(3个数据为一组)该怎么写?
$ctt=0;	foreach($items_sum as &$v) {		if($ctt%3==0){			$v =....;//前两项相除得到第三项		}		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++;	}
登入後複製

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板