简体   繁体   English

在foreach内部指定了foreach

[英]specified foreach inside foreach

i have some hmm ... i think some illogical logic which is i think by myself ... for now i can show you this : 我有一些...我想一些不合逻辑的逻辑,我自己想...现在我可以向您展示:

$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>";
          }

that's will value 那会很有价值

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

I need to calculate the first array till end array (as many $cur_pos_10), but it always recalculate the entire array for the second value to the end. 我需要计算第一个数组直到结束数组(与$ cur_pos_10一样多),但是它总是为第二个值重新计算整个数组。 Please help me, and im really sorry for my bad english. 请帮助我,我真的很抱歉我的英语不好。

I need this : first result calculate array [0] to [13]; 我需要这个:第一个结果计算数组[0]到[13]; second result calculate array [14] only; 仅第二个结果计算数组[14];

Hard to understand, unclear what code should do, but it looks like you just need to define variables for storing results before foreach because $test [] will push value to an array $test and variable $test is defined inside foreach and accumulate all the results over multiple loops. 很难理解,不清楚应该做什么代码,但是看起来您只需要在foreach之前定义用于存储结果的变量,因为$test []会将值推入数组$test而变量$testforeach定义并累积所有多个循环的结果。 So try this: 所以试试这个:

$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>";
}

A well indented and aligned code is important. 缩进和对齐的代码很重要。 I have indented and aligned your code. 我已缩进并对齐您的代码。 As @Kazz indicated the problem is that you are not resetting the $test variable. 正如@Kazz指出的那样,问题在于您没有重置$ test变量。 Some comments in the code will also be valuable. 代码中的一些注释也将很有价值。

<?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>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM