ホームページ >バックエンド開発 >PHPチュートリアル >複数の配列を別の配列に形成する問題
次の配列はプログラム内で形成されます (プログラム内のキーは動的であり、ここではハードコーディングされています)
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)
[2014 年 3 月の店舗数] => 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
[カテゴリ] => 合計
[2014 年 1 月_店舗数] => 331
[2014 年 2 月_店舗数] => 331
[2014 年 3 月_店舗数] ] => 331
)
データを合計配列形式 (3 桁ごとに 1 つ) に設定したい場合、number_format 関数をどこに記述すればよいでしょうか?
計算後、
$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
[April2014_Number_stores] => 10,331
[April2014_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
[2014 年 1 月_店舗数] => 10,331
[2014 年 2 月_店舗数] => 500,331
[2014 年 3 月_店舗数] => 4,512, 331
)
よかったらもう一度質問させてくださいto 項目分割と3番目の項目(3データをグループ)への代入はどのように書くのですか?
$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);
これは通常、関連付けられたキーで操作する必要があります
ヒーロー、詳しく説明してもらえますか...
$ar = array(
'Category' => 'TOTAL',
'1 月の店舗数' => 10331,
'2014 年 2 月の店舗数' => 4512331,
$ar[' 2014 年 3 月_店舗数'] = $ar['2014 年 1 月_店舗数'] / $ar[ 'April2014_Number_stores'];
実際に使用する場合は、除数($ar['April2014_Number_stores'])が0に等しいかどうかを確認する必要があります
計算後、
$ctt=0; foreach($items_sum as &$v) { if($ctt%3==0){ $v =....;//前两项相除得到第三项 } if(is_numeric($v)) $v = number_format($v); $ctt++; }Array をフォーマットします (
[Category] => ; TOTAL
[April2014_Number_stores] = > 10,331
[April2014_Number_stores] => 4,512,331
分割したい場合はもう一度教えてください最初の 2 つの項目を 3 番目の項目に割り当てます(3つのデータをグループとして)どうするか書き込みますか?
りー
りー