php 如何合并 2 个多维数组?
php how do i merge 2 multidimensional arrays?
我有两个数组。列和表信息。
我想将它们合并在一起,但是当我尝试使用 array_merge 进行合并时,我在 var_dump 时得到了这些结果:
array (size=67)
0 => string 'receive_date' (length=12)
1 => string 'day' (length=3)
2 => string 'main_category' (length=13)
3 => string 'brand' (length=5)
4 => string 'first_to_receive_qty' (length=20)
5 => string 'purchase_value' (length=14)
6 =>
array (size=6)
0 => string '2021-11-09' (length=10)
1 => string 'Tuesday' (length=7)
2 => string 'apparel' (length=7)
3 => string 'adidas' (length=6)
4 => string '3184' (length=4)
5 => string '34773.31' (length=8)
7 =>
array (size=6)
0 => string '2021-11-09' (length=10)
1 => string 'Tuesday' (length=7)
2 => string 'apparel' (length=7)
3 => string 'nike' (length=4)
4 => string '642' (length=3)
5 => string '5089.50' (length=7)
8 =>
array (size=6)
0 => string '2021-11-09' (length=10)
1 => string 'Tuesday' (length=7)
2 => string 'apparel' (length=7)
3 => string 'puma' (length=4)
4 => string '15' (length=2)
5 => string '120.00' (length=6)
但我希望数组 var_dump 看起来像:
array (size=726)
0 =>
array (size=6)
'receive_date' => string '2021-11-09' (length=10)
'day' => string 'tuesday' (length=7)
'main_category' => string 'apparel' (length=7)
'brand' => string 'adidas' (length=6)
'first_to_receive_qty' => string '3184' (length=4)
'purchase_value' => string '34773.31' (length=8)
1 =>
array (size=6)
'receive_date' => string '2021-11-09' (length=10)
'day' => string 'tuesday' (length=7)
'main_category' => string 'apparel' (length=7)
'brand' => string 'nike' (length=4)
'first_to_receive_qty' => string '642' (length=3)
'purchase_value' => string '5089.50' (length=7)
2 =>
array (size=6)
'receive_date' => string '2021-11-09' (length=10)
'day' => string 'tuesday' (length=7)
'main_category' => string 'apparel' (length=7)
'brand' => string 'puma' (length=4)
'first_to_receive_qty' => string '15' (length=2)
'purchase_value' => string '120.00' (length=6)
我已经尝试了一些东西,但我似乎无法弄清楚如何将它们合并到一个看起来像这样的数组中。谁能告诉我该怎么做?
您想合并而不是合并。获取新数组:
$result = array_map(function($v) use($columns) {
return array_combine($columns, $v);
}, $tableinfo);
或者简单地循环并合并:
foreach($tableinfo as $row) {
$result[] = array_combine($columns, $row);
}
或修改$tableinfo
:
array_walk(function(&$v) use($columns) {
$v = array_combine($columns, $v);
}, $tableinfo);
或者循环:
foreach($tableinfo as &$row) {
$row = array_combine($columns, $row);
}
我有两个数组。列和表信息。 我想将它们合并在一起,但是当我尝试使用 array_merge 进行合并时,我在 var_dump 时得到了这些结果:
array (size=67)
0 => string 'receive_date' (length=12)
1 => string 'day' (length=3)
2 => string 'main_category' (length=13)
3 => string 'brand' (length=5)
4 => string 'first_to_receive_qty' (length=20)
5 => string 'purchase_value' (length=14)
6 =>
array (size=6)
0 => string '2021-11-09' (length=10)
1 => string 'Tuesday' (length=7)
2 => string 'apparel' (length=7)
3 => string 'adidas' (length=6)
4 => string '3184' (length=4)
5 => string '34773.31' (length=8)
7 =>
array (size=6)
0 => string '2021-11-09' (length=10)
1 => string 'Tuesday' (length=7)
2 => string 'apparel' (length=7)
3 => string 'nike' (length=4)
4 => string '642' (length=3)
5 => string '5089.50' (length=7)
8 =>
array (size=6)
0 => string '2021-11-09' (length=10)
1 => string 'Tuesday' (length=7)
2 => string 'apparel' (length=7)
3 => string 'puma' (length=4)
4 => string '15' (length=2)
5 => string '120.00' (length=6)
但我希望数组 var_dump 看起来像:
array (size=726)
0 =>
array (size=6)
'receive_date' => string '2021-11-09' (length=10)
'day' => string 'tuesday' (length=7)
'main_category' => string 'apparel' (length=7)
'brand' => string 'adidas' (length=6)
'first_to_receive_qty' => string '3184' (length=4)
'purchase_value' => string '34773.31' (length=8)
1 =>
array (size=6)
'receive_date' => string '2021-11-09' (length=10)
'day' => string 'tuesday' (length=7)
'main_category' => string 'apparel' (length=7)
'brand' => string 'nike' (length=4)
'first_to_receive_qty' => string '642' (length=3)
'purchase_value' => string '5089.50' (length=7)
2 =>
array (size=6)
'receive_date' => string '2021-11-09' (length=10)
'day' => string 'tuesday' (length=7)
'main_category' => string 'apparel' (length=7)
'brand' => string 'puma' (length=4)
'first_to_receive_qty' => string '15' (length=2)
'purchase_value' => string '120.00' (length=6)
我已经尝试了一些东西,但我似乎无法弄清楚如何将它们合并到一个看起来像这样的数组中。谁能告诉我该怎么做?
您想合并而不是合并。获取新数组:
$result = array_map(function($v) use($columns) {
return array_combine($columns, $v);
}, $tableinfo);
或者简单地循环并合并:
foreach($tableinfo as $row) {
$result[] = array_combine($columns, $row);
}
或修改$tableinfo
:
array_walk(function(&$v) use($columns) {
$v = array_combine($columns, $v);
}, $tableinfo);
或者循环:
foreach($tableinfo as &$row) {
$row = array_combine($columns, $row);
}