Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[vRegionName] => Boston
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 0
[TotalInvamt] => 0.00
)
[1] => Array
(
[reg_id] => 41
[vRegionName] => Chicago
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 1
[TotalInvamt] => 954.00
)
)
)
Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[TotalInv] => 2
)
[1] => Array
(
[reg_id] => 41
[TotalInv] => 8
)
)
)
我想基于[reg_id]在First数组上添加[TotalInv],我该怎么做?请帮帮我。
我想要这个输出::
Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[vRegionName] => Boston
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 0
[TotalInvamt] => 0.00
[TotalInv] => 2
)
[1] => Array
(
[reg_id] => 41
[vRegionName] => Chicago
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 1
[TotalInvamt] => 954.00
[TotalInv] => 8
)
)
)
提前感谢。 Vims Mak
答案 0 :(得分:0)
我建议在定义第一个数组时将键设置为reg_id。
例如,如果您是从查询结果创建此数组:
$results = array();
while($row = mysql_fetch_assoc($blah)){
$id = $row['reg_id'];
$results[$id] = $row;
}
然后,当您遍历第二个阵列时,您可以轻松更新第一个阵列。
foreach($secondArray as $key => $row){
$id = $row['reg_id'];
$val = $row['TotalInv'];
$first[$id]['TotalInv'] = $val;
}
阵列是一件美丽的事情!希望有所帮助!
-fie
答案 1 :(得分:0)
你的意思是:
foreach($yourArr["RX Housing"] as $key => $val) {
if($val["reg_id"] == 10) {
$yourArr["RX Housing"][$key]["TotalInv"] = 2;
}
else {
$yourArr["RX Housing"][$key]["TotalInv"] = 20;
}
}
希望有所帮助
答案 2 :(得分:0)
试试这个
$a = Array
(
'RX Housing' => Array
(
Array
(
'reg_id' => 63,
'vRegionName' => 'Boston',
'Forwarded' => 1,
'Accepted' => 0,
'Rejected' => 0,
'Dupe' => 0,
'PotentialLease' => 0,
'Lcpc' => 0,
'CloseLead' => 0,
'TotalInvamt' => 0.00
),
Array
(
'reg_id' => 41,
'vRegionName' => 'Chicago',
'Forwarded' => 1,
'Accepted' => 0,
'Rejected' => 0,
'Dupe' => 0,
'PotentialLease' => 0,
'Lcpc' => 0,
'CloseLead' => 1,
'TotalInvamt' => 954.00
)
)
);
$b = Array
(
'RX Housing' => Array
(
Array
(
'reg_id' => 63,
'TotalInv' => 2
),
Array
(
'reg_id' => 41,
'TotalInv' => 8
)
)
);
foreach ($a['RX Housing'] as $value) {
foreach ($b['RX Housing'] as $value1) {
foreach ($value as $key => $result) {
if (array_key_exists($key, $value1)) {
if ($result == $value1[$key]) {
$value = array_merge($value, $value1);
$c['RX Housing'][] = $value;
}
}
}
}
}
var_dump($c);
out put是
array
'RX Housing' =>
array
0 =>
array
'reg_id' => int 63
'vRegionName' => string 'Boston' (length=6)
'Forwarded' => int 1
'Accepted' => int 0
'Rejected' => int 0
'Dupe' => int 0
'PotentialLease' => int 0
'Lcpc' => int 0
'CloseLead' => int 0
'TotalInvamt' => float 0
'TotalInv' => int 2
1 =>
array
'reg_id' => int 41
'vRegionName' => string 'Chicago' (length=7)
'Forwarded' => int 1
'Accepted' => int 0
'Rejected' => int 0
'Dupe' => int 0
'PotentialLease' => int 0
'Lcpc' => int 0
'CloseLead' => int 1
'TotalInvamt' => float 954
'TotalInv' => int 8