Add options from array to subarray from another array in PHP
Clash Royale CLAN TAG#URR8PPP
Add options from array to subarray from another array in PHP
Maybe this one is rather easy for Array wizards, I don't know, but for me it seems to be a though one...
Let's say I have this array $brands_models_options
(I've simplified it with fake categories to make it more understandable):
$brands_models_options
[0] => Array
(
[brand] => ford
[models] => Array
(
[0] => Array
(
[modelname] => focus
[options] => Array
(
[0] => Array
(
[optionname] => gps
)
[1] => Array
(
[optionname] => rims
)
[2] => Array
(
[optionname] => tinted_windows
)
)
)
[1] => Array
(
[modelname] => fiesta
[options] => Array
(
[0] => Array
(
[optionname] => radio
)
[1] => Array
(
[optionname] => rims
)
)
)
)
[1] => Array
(
[brand] => volvo
[models] => Array
(
[0] => Array
(
[modelname] => v40
[options] => Array
(
[0] => Array
(
[optionname] => led_lights
)
[1] => Array
(
[optionname] => rims
)
[2] => Array
(
[optionname] => automatic_screen_wipers
)
)
)
[1] => Array
(
[modelname] => v60
[options] => Array
(
[0] => Array
(
[optionname] => automatic_screen_wipers
)
[1] => Array
(
[optionname] => panoramic_roof
)
)
)
)
[2] => Array
(
[brand] => some_fake_car_brand_that_has_no_models
)
[…] => Array
( …
Next, I have another array $colors_for_options
:
$colors_for_options
[0] => Array
(
[brand] => volvo
[color] => blue
[material] => sticker
[available_for] => led_lights, panoramic_roof
)
[1] => Array
(
[brand] => ford
[color] => red
[material] => sticker
[available_for] => gps,rims
)
[2] => Array
(
[brand] => ford
[color] => blue
[material] => paint
[available_for] => gps,rims,tinded_windows
)
[3] => Array
(
[brand] => volvo
[color] => black
[material] => paint
[available_for] => rims, automatic_screen_wipers
)
…
I'm looking for a way to merge these arrays into $brands_models_options_colors
, and put the available colors and their material at the correct options:
$brands_models_options_colors
[0] => Array
(
[brand] => ford
[models] => Array
(
[0] => Array
(
[modelname] => focus
[options] => Array
(
[0] => Array
(
[optionname] => gps
[availablecolors] => Array
(
[0] => Array
(
[colorname] => red
[material] => sticker
)
[1] => Array
(
[colorname] => blue
[material] => paint
)
)
)
[1] => Array
(
[optionname] => rims
[availablecolors] => Array
(
[0] => Array
(
[colorname] => red
[material] = sticker
)
[1] => Array
(
[colorname] => blue
[material] => paint
)
)
)
[2] => Array
(
[optionname] => tinted_windows
[availablecolors] => Array
(
[0] => Array
(
[colorname] => blue
[material] => paint
)
)
)
)
)
[1] => Array
(
[modelname] => fiesta
[options] => Array
(
[0] => Array
(
[optionname] => radio
)
[1] => Array
(
[optionname] => rims
[availablecolors] => Array
(
[0] => Array
(
[colorname] => red
[material] => sticker
)
[1] => Array
(
[colorname] => blue
[material] = paint
)
)
)
)
)
)
[1] => Array
(
[brand] => volvo
[models] => Array
(
[0] => Array
(
[modelname] => v40
[options] => Array
(
[0] => Array
(
[optionname] => led_lights
[availablecolors] => Array
(
[0] => Array
(
[colorname] => blue
[material] => sticker
)
)
)
[1] => Array
(
[optionname] => rims
[availablecolors] => Array
(
[0] => Array
(
[colorname] => black
[material] => paint
)
)
)
[2] => Array
(
[optionname] => automatic_screen_wipers
[availablecolors] => Array
(
[0] => Array
(
[colorname] => black
[material] => paint
)
)
)
)
)
[1] => Array
(
[modelname] => v60
[options] => Array
(
[0] => Array
(
[optionname] => automatic_screen_wipers
[availablecolors] => Array
(
[0] => Array
(
[colorname] => black
[material] => paint
)
)
)
[1] => Array
(
[optionname] => panoramic_roof
[availablecolors] => Array
(
[0] => Array
(
[colorname] => blue
[material] => sticker
)
)
)
)
)
)
[2] => Array
(
[brand] => some_fake_car_brand_that_has_no_models
)
[…] => Array
( …
I have been looking online and at other stackoverflow question, but it's always something a bit different so I don't succeed... any help is much appreciated!
I was thinking about splicing the $colors_for_options
array:
$colors_for_options
$colors_for_options[$i]['available_for'] = explode(',',$colors_for_options[$i]['available_for']);
but I'm having trouble on how to do the foreach loop (I guess that's what I need) to insert the colors into the other array:
$brands_models_options = array_combine(array_column($brands_models_options, 'brand'), $brands_models_options);
$brands_models_options = array_combine(array_column($brands_models_options['models'], 'optionname'), $brands_models_options['models']);
foreach ($colors_for_options as $k => $item) {
if (isset($brands_models_options[$item['brand']])) {
foreach ($brands_models_options[$item['brand']]['models'] as &$sort)
if ($sort['model'] == $item['available_for'])
$sort['availablecolors'] = $item;
If you can't find any ready solution, think about developing it by yourself. Then, if you have any errors or code's strange behavior, post it here and we'll help you to fix it. But we won't write code for you.
– Nikita Leshchev
Aug 12 at 14:30
@Nikita makes sense. Tried a bunch of approaches and I'll add the errors to the questions later. I'm not familiar enough with the php commands to debug it myself I'm afraid, that's why I posted this question.
– binoculars
Aug 12 at 14:38
@binoculars no problem. Just post some of your code, which you want to edit and we'll help you
– Nikita Leshchev
Aug 12 at 14:44
I was thinking about using
explode
to split the $colors_for_options
array (cf. updated question)– binoculars
Aug 12 at 17:52
explode
$colors_for_options
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
how would you do it manually on paper?
– Juan
Aug 12 at 14:27