I have two array, one array has color and another array has fruits, here I want to combine using matching color reference. How to combine by using array reference?
$fruits = ['yellow', 'green', 'orange'];
$relatedFurites = [
['yellow'=>'banana', 'green'=>'avacado'],
['yellow'=>'mango', 'green'=> 'chilli']
];
expected output by using array refernce
$output = [
'yellow'=>['banana', 'mango'],
'green'=>['avacado', 'chilli']];
Thanks for all suggestions.
If the $fruits array is related as I asked in comments then you can use array_column and you won't have to iterate every item in the array.
foreach($fruits as $color){
$output[$color] = array_column($relatedFurites, $color);
}
var_dump($output);
You could build the $output array using a nested foreach
foreach ( $relatedFurites as $keyFruites => $valueFruites) {
foreach( $valueFruites as $key => $value){
$output[$key][] = $value;
}
}