结合两个不同的维度数组PHP [英] Combine two different dimensional arrays PHP
本文介绍了结合两个不同的维度数组PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个不同的维数组.
I have two different dimensional arrays.
数组1:
Array1
(
[0] => Array
(
[id] => 123
[price] => 5
[purchase_time] => 2014/4/10
)
[1] => Array
(
[id] => 123
[price] => 5
[purchase_time] => 2014/5/17
)
)
数组2:
Array2
(
[0] => 5
[1] => 8
)
我想要这样的东西:
Array
(
[0] => Array
(
[id] => 123
[price] => 5
[purchase_time] => 2014/4/10
[Qty] => 5
)
[1] => Array
(
[id] => 123
[price] => 5
[purchase_time] => 2014/5/17
[Qty] => 8
)
)
基本上,第一个数组是我从SQL表获得的信息.第二个数组包含所售产品的数量.我现在想将这两个数组组合在一起,并使用组合后的数组创建一个新表.由于这两个阵列具有不同的尺寸.我不确定该怎么做.这是我的尝试:
Basically the first array is the information I got from a SQL table. The second array contains the quantity for the products sold. I now want to combine these two array together and use the combined array to create a new table. Since these two arrays have different dimensions. I'm not sure how to do it. Here is my try:
$i = 0;
foreach($array1 as $row)
{
$newarray = array_merge($row,$array2[$i]);
$i++;
}
推荐答案
可能是更简单的方法,但是如果没有foreach
,这很有趣:
Might be a simpler way, but for fun without foreach
:
array_walk($array1, function(&$v, $k, $a){ $v['Qty'] = $a[$k]; }, $array2);
这篇关于结合两个不同的维度数组PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文