通过键合并(合并)2个数组,并在结果数组中更改键名称

时间:2019-02-27 09:32:47

标签: php arrays

我将这两个数组作为输出:

Value Array
(
    [0] => 10100153
    [1] => 2007
    [2] => 350
    [3] => 804082
    [4] => WW006
    [5] => WHT/NNY/OXGM
    [6] => 35/38
    [7] => 804082         WW00635/38
    [8] => 0,00138857
    [9] => Champion 3pk Quarter Socks
)
Numbers Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10 
)

我想将它们组合起来,然后将value数组的键值更改为value,将numbers数组更改为数字,所以看起来像这样:

期望输出

['Value' => '10100153', 'Number' => 1],
['Value' => '2007', 'Number' => 2],
['Value' => '390', 'Number' => 3],
['Value' => '804715', 'Number' => 4],
['Value' => 'WW001', 'Number' => 5],
['Value' => 'WHT/WHT/WHT', 'Number' => 6],
['Value' => '39/42', 'Number' => 7],
['Value' => '804715         WW00139/42', 'Number' => 8],
['Value' => '0.00138857', 'Number' => 9],
['Value' => '3pk Quarter Socks', 'Number' => 10]

我只能找到array_combinearray_merge,但是array_merge只是将数字数组添加到值数组的末尾,而array_combine将数字添加到值数组的末尾。值数组文本的结尾

3 个答案:

答案 0 :(得分:8)

您可以将array_mapdoc)和array_combine doc用作:

$a1 = ["10100153", "2007", "350", "804082", "WW006", "WHT/NNY/OXGM", "35/38", "804082       WW00635/38", "0,00138857", "Champion 3pk Quarter Socks"];
$a2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$res = array_map(null, $a1, $a2);
$keys = array("Value", "Number");
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res);

通知null中使用array_map。从文档中:

  

此函数的一个有趣用途是构造一个数组数组,可以通过使用NULL作为回调函数的名称来轻松实现

这样,您可以合并更多数组-只需记住将正确的键添加到$keys

答案 1 :(得分:2)

您可以使用常规的foreach循环来迭代您的values数组。在values数组中的每个元素上,您都可以使用当前索引在numbers数组中获取其对应的元素。

在每次迭代中(值数组的每个循环),您都可以将关联数组添加到结果数组中(在这里我将其称为$res)。

请参见以下示例:

$values = ["10100153", "2007", "350", "804082", "WW006", "WHT/NNY/OXGM", "35/38", "804082       WW00635/38", "0,00138857", "Champion 3pk Quarter Socks"];
$nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$res = []; // create empty array to hold associative arrays
foreach($values as $i=>$val) { // loop over your values array, where the index is $i and the value is $val
    $num = $nums[$i]; // get the number at the given index
    $res[$i] = ["Value" => $val, "Number" => $num]; // set the index in the resulting array to hold a newly formed associative array 
}
print_r($res); // print the results

答案 2 :(得分:0)

通过将两个输入数组都传递到array_map(null...)并使用splat运算符来接收函数内部的参数,可以跳过dWinder的答案中所示的额外循环(与array_map()进行转换)。我的代码片段将具有dWinder解决方案计算复杂度的一半-只需一个循环,而不是两个循环。

代码:(Demo

$values = ["10100153", "2007", "350", "804082", "WW006", "WHT/NNY/OXGM", "35/38", "804082 WW00635/38", "0,00138857", "Champion 3pk Quarter Socks"];
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$keys = ["Value", "Number"];
var_export(
    array_map(function(...$data) use ($keys) {
        return array_combine($keys, $data);
    }, $values, $numbers)
);

输出:

array (
  0 => 
  array (
    'Value' => '10100153',
    'Number' => 1,
  ),
  1 => 
  array (
    'Value' => '2007',
    'Number' => 2,
  ),
  2 => 
  array (
    'Value' => '350',
    'Number' => 3,
  ),
  ...you get the point
)