通过键合并(合并)2个数组,并在结果数组中更改键名称 [英] Combine (merge) 2 arrays by keys and change keys name in the result array
问题描述
我将这两个数组作为输出:
I have these two arrays as output:
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数组的键值,所以看起来像这样:
I want to combine them and change the key value of the value array in value and the numbers array in numbers, so it looks something like this:
需求输出
['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_combine
和 array_merge
,但是 array_merge
只是将数字数组添加到值数组的末尾,而 array_combine
将数字添加到值数组文本的末尾
All I can find is array_combine
and array_merge
, but array_merge
just adds the numbers array to the end of the value array, and array_combine
adds the numbers to the end of the text of the value array
推荐答案
array_map
( doc )和 array_combine
( doc )的格式为:
You can use array_map
(doc) and array_combine
(doc) as:
$res = array_map(null, $valuesArray, $numbersArray);
$keys = array("Value", "Number");
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res);
注意使用 null $
array_map
中的c $ c>。从文档中:
Notice the use of null
in array_map
. From documentation:
此函数的一个有趣用法是构造一个数组数组,可以通过使用NULL作为名称来轻松执行该数组。回调函数的功能
An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function
这样,您可以合并更多数组-只需记住将正确的键添加到 $键
This way you can merge more arrays - just remember to add the correct key to $keys
实时示例: 3v4l
这篇关于通过键合并(合并)2个数组,并在结果数组中更改键名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!