Rumah >pembangunan bahagian belakang >tutorial php >多个数组形成另一个数组的问题
程序中形成了以下的数组(程序的中的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)
$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
$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
算完了再格式化
$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
算完了再格式化
$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
$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
$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++; }