2016-11-01 11 views
0

内側のforeach ...今の を指定:は、私はあなたにこれを示すことができ、私はいくつかうーん...私は自分で考えているいくつかの非論理的なロジックを考える必要がありますforeach

$new ['period_10th'] = $periodic_items [9] ; 
      foreach ($new ['period_10th'] as $item) { $po_10th [] = $item -> po_id ; } 
      $new ['uni_10'] = array_unique($po_10th) ; 
      foreach ($new ['uni_10'] as $po_id) { 
      $po = $this -> model_prcsys -> get_po_by_id (md5($po_id)) ; 
      $pos_10 [] = $po ['po_id'] ; 
      $currency10 [] = $po ['currency'] ; 
      } 
      $cur_pos_10 = array_unique($currency10) ; 
      foreach ($cur_pos_10 as $currency) { 
      $new ['po_arr_10'] = $this -> model_prcsys -> get_pos_with_curr ($pos_10,$currency) ; 
       foreach ($new ['po_arr_10'] as $key) { 
       $test [] = $key -> total_line_price; 
       } 
       print_r($test);echo "<br><br>"; 
       print_r(array_sum($test));echo "<br><br>"; 
      } 

だが

を大切になること
Array ([0] => 20700000.00 [1] => 10340000.00 [2] => 4160000.00 [3] => 8150000.00 [4] => 9065000.00 [5] => 3500000.00 [6] => 2530000.00 [7] => 650000.00 [8] => 4395000.00 [9] => 17100000.00 [10] => 11250000.00 [11] => 6900000.00 [12] => 300000.00 [13] => 15750000.00) 

114790000 

Array ([0] => 20700000.00 [1] => 10340000.00 [2] => 4160000.00 [3] => 8150000.00 [4] => 9065000.00 [5] => 3500000.00 [6] => 2530000.00 [7] => 650000.00 [8] => 4395000.00 [9] => 17100000.00 [10] => 11250000.00 [11] => 6900000.00 [12] => 300000.00 [13] => 15750000.00 [14] => 1200000.00) 

115990000 

最初の配列を($ cur_pos_10のように)終わりの配列まで計算する必要がありますが、常に配列全体を2番目の値の最後まで再計算します。私を助けてください、私の悪い英語のために本当に残念です。

私はこれが必要です: 最初の結果は配列[0]から[13]を計算します。 2番目の結果は配列[14]のみを計算します。

答えて

1

よくインデントされ、整列したコードが重要です。インデントしてコードを整列させました。 @Kazzは$ test変数をリセットしていないということを示しています。コード内のいくつかのコメントも貴重です。

<?php 

$new['period_10th'] = $periodic_items[9] ; 
foreach ($new['period_10th'] as $item) { 
    $po_10th[] = $item->po_id ; 
} 

$new['uni_10'] = array_unique($po_10th) ; 
foreach ($new['uni_10'] as $po_id) { 
    $po   = $this->model_prcsys->get_po_by_id (md5($po_id)) ; 
    $pos_10[]  = $po ['po_id'] ; 
    $currency10[] = $po ['currency'] ; 
} 

$cur_pos_10 = array_unique($currency10) ; 
foreach ($cur_pos_10 as $currency) { 
    $new['po_arr_10'] = $this->model_prcsys->get_pos_with_curr ($pos_10,$currency) ; 

    $test = array(); // this was missing and probably causing problems 
    foreach ($new['po_arr_10'] as $key) { 
     $test[] = $key->total_line_price; 
    } 

    print_r($test); 
    echo "<br><br>"; 
    print_r(array_sum($test)); 
    echo "<br><br>"; 
} 
1

何をすべきかをコード不明で、理解しにくいが、あなただけの$test []が配列$testと変数$testに値をプッシュしますのでforeach前に結果を格納するための変数を定義する必要があるように見えますがforeach内で定義されており、すべての結果を蓄積しています複数のループにわたって。したがって、これを試してみてください:

$new ['period_10th'] = $periodic_items [9] ; 
foreach ($new ['period_10th'] as $item) { $po_10th [] = $item -> po_id ; } 
$new ['uni_10'] = array_unique($po_10th) ; 

$pos_10 = []; // added 
$currency10 = []; // added 
foreach ($new ['uni_10'] as $po_id) { 
    $po = $this -> model_prcsys -> get_po_by_id (md5($po_id)) ; 
    $pos_10 [] = $po ['po_id'] ; // possibly similar problem if whole code is in loop or is executed multiple times 
    $currency10 [] = $po ['currency'] ; // possibly similar problem if whole code is in loop or is executed multiple times 
} 

$cur_pos_10 = array_unique($currency10) ; 
foreach ($cur_pos_10 as $currency) { 
    /* in your code 
     in first iteration variable $test is undefined 
     in second iteration variable $test is defined and count($test) == 14 
    */ 
    $new ['po_arr_10'] = $this -> model_prcsys -> get_pos_with_curr ($pos_10,$currency) ; 
    $test = []; // added, variable $test is set to an empty array and erase previous results 
    foreach ($new ['po_arr_10'] as $key) { 
    $test [] = $key -> total_line_price; // problem was here 
    } 
    print_r($test);echo "<br><br>"; 
    print_r(array_sum($test));echo "<br><br>"; 
} 
+0

ありがとう、私は$テストをリセットしていない – Mauliardiwinoto

関連する問題