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

WBOY
发布: 2016-06-23 13:59:44
原创
813 人浏览过

程序中形成了以下的数组(程序的中的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
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板