Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine (merge) 2 arrays by keys and change keys name in the result array

Tags:

arrays

php

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

like image 485
FlubberBeer Avatar asked Dec 24 '22 00:12

FlubberBeer


2 Answers

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

like image 153
dWinder Avatar answered Mar 31 '23 03:03

dWinder


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
like image 33
Nick Parsons Avatar answered Mar 31 '23 01:03

Nick Parsons