FlubberBeer
FlubberBeer

Reputation: 105

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 
)

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:

Desire output

['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]

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

Upvotes: 4

Views: 2350

Answers (3)

mickmackusa
mickmackusa

Reputation: 48100

You can skip the extra round of iterating (while transposing with array_map(null...)) as demonstrated in dWinder's answer by passing both input arrays into array_map() and using the splat operator to receive the arguments inside the function. My snippet will have half of the computational complexity of dWinder's solution -- just one loop instead of two.

From PHP7.4 (Demo)

$keys = ["Value", "Number"];
var_export(
    array_map(fn(...$data) => array_combine($keys, $data), $values, $numbers)
);

Before PHP7.4: (Demo) (or with compact())

$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)
);

Output:

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

Upvotes: 0

dWinder
dWinder

Reputation: 11642

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);

Notice the use of null in array_map. From documentation:

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

Live example: 3v4l

Upvotes: 8

Nick Parsons
Nick Parsons

Reputation: 50974

You could use a regular foreach loop to iterate over your values array. At each element in the values array you can get its corresponding element in the numbers array by using the current index.

At each iteration (each loop of your values array) you can add an associative array into a resulting array (here I called it $res).

See example below:

$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

Upvotes: 2

Related Questions