I need to merge (sort of join) two arrays with both same keys, and in result i want to have the values of the first as keys of the second one :
Example :
$keyArray = [ "key1" => "map1", "key2" => "map1", "key3" => "map2", "key4" => "map3" ]; $valuesArray = [ "key1" => "value1", "key2" => "value2", "key3" => "value3", "key4" => "value3" ]; // expected result :
$mappedResultArray = [ "map1" => [ "value1", "value2" ], "map2" => [ "value3" ], "map3" => [ "valu3" ], ]; I know that this is possible by using php loops/foreach through both arrays, But I want to have a solution using PHP array_* functions (array_map, array_merge ....)
array_functions do not consider array keys. This means that you'll have to use something likearray_keyswhich is useless.array_combine()is the closest but it doesn't handle the merging of multiple values for one same key, so typically you would only get'map1' => 'value2'instead of'map1' => ['value1', 'value2']. So I think you'll probably have to write your own code to do this "merging" if the key already has a value for it.foreachis the most performant solution because it would be aO(N)solution, no need to play smart other way.