Heim > Backend-Entwicklung > PHP-Tutorial > 多个数组形成另一个数组的问题

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

WBOY
Freigeben: 2016-06-23 13:59:44
Original
847 Leute haben es durchsucht

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

Array(    [Category] => TOTAL    [January2014_Number_stores] => 0    [February2014_Number_stores] => 0    [March2014_Number_stores] => 0)
Nach dem Login kopieren

又有数组
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)
Nach dem Login kopieren

现在要把第二组中的数组中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);
Nach dem Login kopieren
Nach dem Login kopieren
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);
Nach dem Login kopieren
Nach dem Login kopieren
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);
Nach dem Login kopieren
Nach dem Login kopieren
Nach dem Login kopieren
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);
Nach dem Login kopieren
Nach dem Login kopieren
Nach dem Login kopieren
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++;	}
Nach dem Login kopieren
Nach dem Login kopieren

这一般应该用关联键操作

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

$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);
Nach dem Login kopieren
Nach dem Login kopieren
Nach dem Login kopieren
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++;	}
Nach dem Login kopieren
Nach dem Login kopieren

$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++;	}
Nach dem Login kopieren

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage