I have two arrays. One is a list of colors, the second is an associative array of key value pairs. My objective is to take the key value pairs from the associative array and make them a sub-array of each item in the colors array. Searching SO has gotten me various adjacent issues, but not the one I'm having specifically. Here are two example arrays, and then $final is what I want to achieve:
$colors = ['#eea845', '#64A0B4', '#003c50', '#FF5568', '#eee', '#5cb85c', '#5bc0de', '#f0ad4e', '#d9534f'];
$test = [
'key1' => 'val1',
'key2' => 'val2',
'key3' => 'val3',
'key4' => 'val4',
'key5' => 'val5',
'key6' => 'val6',
'key7' => 'val7',
'key8' => 'val8',
'key9' => 'val9',
];
$final = [
'#eea845' => [
'name' => 'key1',
'value' => 'val1',
],
'#64A0B4' => [
'name' => 'key2',
'value' => 'val2',
],
etc.....
]
I've been looking at array_walk, array_map, and trying to figure out how to combine for and foreach loops. I looked at the answer given here (Append one array values as key value pair to another array php) but I'm not sure how to use it on an already existing array and be able to get the index of each. For example, that solution uses array_walk($array1, function(&$v, $k) use($array2) { $v['date'] = $array2[$k]; });
but I need to have the values from $array2 be added to already existing items in $array1, and while I tried doing function($i, $v, $k)
with $i being the index inside $array1, that didn't work, $i being undefined.
I'm stumped and not sure where to look next. How would you
return $colors[$i] =>
[
'name' => $test[$key],
'value' => $test[$name]
]
?
(For context, I am using this to get values to input into a Twig template, and this looks like the best way to do it for that half of the problem. But if it's too difficult on this side...)
CodePudding user response:
I'd just use a combination of foreach()
using the below logic:
- Check if both the arrays have same length.
- Use
foreach
to walk through the array and fill in the final array. - I am not good in this point, but I use a
$count
to keep track of the index. You can use cursors likenext($colors)
andcurrent($colors)
, but I am not sure.
<?php
$colors = [
'#eea845', '#64A0B4', '#003c50', '#FF5568', '#eee', '#5cb85c', '#5bc0de', '#f0ad4e', '#d9534f'
];
$test = [
'key1' => 'val1',
'key2' => 'val2',
'key3' => 'val3',
'key4' => 'val4',
'key5' => 'val5',
'key6' => 'val6',
'key7' => 'val7',
'key8' => 'val8',
'key9' => 'val9',
];
if (count($colors) == count($test)) {
$count = 0;
$finalOne = array();
foreach ($test as $key => $value) {
$finalOne[$colors[$count]] = [
"name" => $key,
"value" => $value
];
$count ;
}
print_r($finalOne);
} else {
echo "Arrays are of not same length.";
}
Output
Array
(
[#eea845] => Array
(
[name] => key1
[value] => val1
)
[#64A0B4] => Array
(
[name] => key2
[value] => val2
)
[#003c50] => Array
(
[name] => key3
[value] => val3
)
[#FF5568] => Array
(
[name] => key4
[value] => val4
)
[#eee] => Array
(
[name] => key5
[value] => val5
)
[#5cb85c] => Array
(
[name] => key6
[value] => val6
)
[#5bc0de] => Array
(
[name] => key7
[value] => val7
)
[#f0ad4e] => Array
(
[name] => key8
[value] => val8
)
[#d9534f] => Array
(
[name] => key9
[value] => val9
)
)
Working Demo: http://sandbox.onlinephpfunctions.com/code/73fb63fab8ccbdb6f73c795572ae51a0d98acdb4
CodePudding user response:
There are several ways to do it. One way is to use foreach
on the associative array, so you get key and value in separate variables, and to use next()
to get the corresponding value from the first (indexed) array:
foreach ($test as $key => $value) {
$final[current($colors)] = ["name" => $key, "value" => $value];
next($colors);
}
In the rare event you had already used next()
on $colors
, you'll have to call reset($colors)
before starting this loop.