合并数组合并为一个,同时保留键的值=>价值PHP [英] Merging arrays into one, while retaining the values of key=>value PHP

查看:143
本文介绍了合并数组合并为一个,同时保留键的值=>价值PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我问了一个问题关于<一个href=\"http://stackoverflow.com/questions/19820836/merging-the-values-of-each-array-in-an-array-of-arrays-in-php#19820981\">merging数组在PHP 数组的数组和价值观得到正确的答案,它是如何以往任何时候都带着问题,那些我想不通无论VAR转储或回声我的地方。

I recently asked a question about,merging the values of arrays in an array of arrays in php and got the correct answer, how ever it came with problems, ones I cannot figure out regardless of the var dumps or echos I place.

使用相同的数组示例,并且将溶液提供,如果我做:

Using the same array example, and the solution provided if I do:

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

(同时牢记,这很像previous问题,上面的数组可能有它内部的70阵列)

(while keeping in mind, that much like the previous question, the above array might have 70 arrays inside of it)

我回来是这样的:

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

一个数组,数组的previous阵列的所有阵列COM pressed和 $键=&GT; $值的加在一起

请告诉我这个问题?的regardelss,我的var_dump 回声,无论是 $键 $值甚至 $ innerArray

Whats the issue? regardelss of where I var_dump or echo, be it the $key, $value or even the $innerArray

我最终得到说法万吨通知:

I end up getting tons of notices saying:

注意:未定义指数:ticket_labor_ot_travel_hours在
   C:\\ XAMPP \\ htdocs中\\ RMS \\网站\\网络\\模块\\报告\\控制器\\ Index.controller.php
  在线 146

Notice: Undefined index: ticket_labor_ot_travel_hours in C:\xampp\htdocs\rms\site\web\module\Report\controller\Index.controller.php on line 146

每个通知是在 $ innerArray 每个键的不同。所以我认为让这样做如果(使用isset($键)及和放大器;使用isset($值))。{...} 没有,同样的问题。

each notice is different for each key in the $innerArray. So I thought lets do if(isset($key) && isset($value)){ ... } Nope, same issue.

我已经检查了外阵列,可以gaurentee是什么进来的是我想要的,所有的按键都设置。

I have checked the outer array and can gaurentee that whats coming in is what I want, all the keys are set.

如果你想知道的行146 $结果[$关键] = number_format($结果[$键] + $值,2);

任何帮助吗?

推荐答案

首先,以解决您未定义指数的问题,上面添加该行的支票:

Firstly, to fix your undefined index problem, add a check above that line:

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

二,阵列正在压缩的原因是因为你在循环两层深,你的 $结果数组只有一个很深。我猜你想的的值每一次迭代,并在年底将其输出......像这样做:

Second, the reason your array is compacting is because you're looping two levels deep and your $results array is only one deep. I'm guessing you want to sum the values each iteration and output it at the end... Do it like this:

$result[$key] += $value;

把你的 number_format 的功能,当你输出数据上。

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

这篇关于合并数组合并为一个,同时保留键的值=&GT;价值PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆