>  기사  >  백엔드 개발  >  多个数组形成另一个数组的问题

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

WBOY
WBOY원래의
2016-06-23 13:59:44816검색

程序中形成了以下的数组(程序的中的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++;	}

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.