我有2个数组,想要添加密钥&一个数组的值到另一个数组的每个成员中。 第一个数组是:
Array
(
[0] => Array
(
[supply_id] => 2
)
[1] => Array
(
[supply_id] => 4
)
[2] => Array
(
[supply_id] => 5
)
)
第二个数组是:
Array
(
[status] => 1
[t1_id] => 59
)
我需要的结果是:
Array
(
[0] => Array
(
[supply_id] => 2,
[status] => 1,
[t1_id] => 59,
)
[1] => Array
(
[supply_id] => 4,
[status] => 1,
[t1_id] => 59,
)
[2] => Array
(
[supply_id] => 5,
[status] => 1,
[t1_id] => 59,
)
)
我觉得这很容易,但我无法解决,任何人都可以帮助我吗?
答案 0 :(得分:2)
你可以试试这个:
$res = array();
foreach($secondArray as $k => $v){
$res[$k] = array_merge($secondArray[$k], $firstArray[$k]);
}
答案 1 :(得分:1)
这里我们使用简单的foreach
循环来实现所需的输出。
foreach($firstArray as $key => &$value)
{
$value= array_merge($value,$secondArray);
}
print_r($array);
答案 2 :(得分:1)
循环遍历第一个数组,然后合并新数组中的值:
$array_1= array
(
0 => array
(
"supply_id" => 2
),
1 => array
(
"supply_id" => 4
),
2 => array
(
"supply_id" => 5
),
);
$array_2=array
(
"status" => 1,
"t1_id" => 59
);
$new_array = array();
foreach ($array_1 as $key => $value) {
$new_array[] = array_merge($value,$array_2);
}
var_dump($new_array);
O / P:
array (size=3)
0 =>
array (size=3)
'supply_id' => int 2
'status' => int 1
't1_id' => int 59
1 =>
array (size=3)
'supply_id' => int 4
'status' => int 1
't1_id' => int 59
2 =>
array (size=3)
'supply_id' => int 5
'status' => int 1
't1_id' => int 59