0

我最近问了一个问题,将数组的值合并到 php 中的数组数组中并得到了正确答案,它是如何出现问题的,无论我放置的 var 转储或回声如何,我都无法弄清楚。

使用相同的数组示例,如果我这样做提供了解决方案:

$result = array();
if(is_array($ticketLabors) && !empty($ticketLabors)){
    foreach ($ticketLabors as $innerArray) {
        foreach ($innerArray as $key=>$value) {
            $result[$key] = number_format($result[$key] + $value, 2);
        }
    }
}
var_dump($result);

在这样的数组上:

array(2) {
  [0]=>
  array(10) {
    ["ticket_labor_ot_travel_c"]=>
    string(5) "34.50"
    ["ticket_labor_travel_c"]=>
    string(5) "23.00"
    ["ticket_labor_ot_c"]=>
    string(5) "34.50"
    ["ticket_labor_reg_c"]=>
    string(5) "23.00"
    ["ticket_labor_user_id"]=>
    string(3) "319"
    ["ticket_labor_tot_hours"]=>
    string(4) "0.50"
    ["ticket_labor_reg_hours"]=>
    string(4) "0.50"
    ["ticket_labor_ot_hours"]=>
    string(4) "0.00"
    ["ticket_labor_travel_hours"]=>
    string(4) "0.00"
    ["ticket_labor_ot_travel_hours"]=>
    string(4) "0.00"
  }
  [1]=>
  array(10) {
    ["ticket_labor_ot_travel_c"]=>
    string(4) "0.00"
    ["ticket_labor_travel_c"]=>
    string(4) "0.00"
    ["ticket_labor_ot_c"]=>
    string(4) "0.00"
    ["ticket_labor_reg_c"]=>
    string(4) "0.00"
    ["ticket_labor_user_id"]=>
    string(1) "0"
    ["ticket_labor_tot_hours"]=>
    string(4) "0.00"
    ["ticket_labor_reg_hours"]=>
    string(4) "0.00"
    ["ticket_labor_ot_hours"]=>
    string(4) "0.00"
    ["ticket_labor_travel_hours"]=>
    string(4) "0.00"
    ["ticket_labor_ot_travel_hours"]=>
    string(4) "0.00"
  }
}

(请记住,这很像上一个问题,上面的数组里面可能有 70 个数组)

我得到类似的东西:

array(10) {
  ["ticket_labor_ot_travel_c"]=>
  string(5) "0.00"
  ["ticket_labor_travel_c"]=>
  string(5) "0.00"
  ["ticket_labor_ot_c"]=>
  string(5) "0.00"
  ["ticket_labor_reg_c"]=>
  string(5) "0.00"
  ["ticket_labor_user_id"]=>
  string(5) "0.00"
  ["ticket_labor_tot_hours"]=>
  string(4) "0.00"
  ["ticket_labor_reg_hours"]=>
  string(4) "0.00"
  ["ticket_labor_ot_hours"]=>
  string(4) "0.00"
  ["ticket_labor_travel_hours"]=>
  string(4) "0.00"
  ["ticket_labor_ot_travel_hours"]=>
  string(4) "0.00"
}

一个数组,压缩了前一个数组数组的所有数组,并将它们$key=>$value的 ' 添加在一起。

什么问题?不管我在哪里,var_dump或者echo$key$value甚至是$innerArray

我最终收到大量通知说:

注意:未定义索引:第 146 行 C:\xampp\htdocs\rms\site\web\module\Report\controller\Index.controller.php 中的ticket_labor_ot_travel_hours

中的每个键的每个通知都不同$innerArray。所以我想让我们做if(isset($key) && isset($value)){ ... }不,同样的问题。

我已经检查了外部数组,并且可以保证进来的就是我想要的,所有的键都设置好了。

如果您想知道第 146 行是什么:$result[$key] = number_format($result[$key] + $value, 2);

有什么帮助吗?

4

4 回答 4

0

首先,要修复未定义的索引问题,请在该行上方添加一个检查:

if(!array_key_exists($key, $result))
    $result[$key] = 0;

其次,你的数组被压缩的原因是你循环了两层深,而你的$results数组只有一层。我猜你想对每次迭代的值求和并在最后输出......这样做:

$result[$key] += $value;

Put your number_format function on when you're outputting the data.

于 2013-11-06T20:13:05.360 回答
0

This is happening first time you access $result[$key] for reading while, it was not initialized. in order to fix that you need to add a check on it if it is not initialized you replace it by 0.

$result[$key] = number_format((array_key_exists($key, $result)?$result[$key]:0) + $value, 2);

Difference between isset and array_key_exists :

$a = array('key1' => 'フーバー', 'key2' => null);

isset($a['key1']);             // true
array_key_exists('key1', $a);  // true

isset($a['key2']);             // false
array_key_exists('key2', $a);  // true

Taken from : Difference between isset and array_key_exists

于 2013-11-06T20:13:15.177 回答
0

Try replacing that line that is erroring with:

$result[$key] = isset($result[$key]) ? $result[$key] + $value : 0;

You DON'T want to number format it at that point because once you get to the thousands, you can't add 1,000 + 1 because of the ,

It's true in that you could set the end parameter in the number_format function to not use the thousands separator but it will start increasing the complexity and decrease the performance (since why call a method repeatedly when you aren't properly making use of the functions output until the final call to the function on the last iteration and why increase the complexity when it's not needed at that point in the code?).

You will want to number format it right at the end for example when you display it to the end user. If you want to keep precision to 2 decimal places during the calculation. You are ok to use round($result[$key] + $value, 2) instead.

于 2013-11-06T20:15:57.013 回答
0

Instead of

$result = array();

use

$result = array_shift($ticketLabors);

This will initialize $result with the first subarray of $ticketLabors, while removing it from the latter.

But you'd better put it right after if.

于 2013-11-06T20:24:43.833 回答