Here is a simple problem that I was unable to solve with php.I want to convert the first array to something like this: ["BMW"=>["g4","g3"], "Mercedes"=>["f1"]]
$array = ["F1" => "Mercedes", "g3"=>"BMW", "g4"=>"BMW"];
$newArray = [];
foreach($array as $key=>$value){
if(!in_array($value, $newArray){
$element = $value => [$key];
array_push($newArray, $element);
} else {
array_push($newArray[$value],$key);
}
}
$element = $value =>[]; on line 6 was my intuitive solution but is invalid.
Am I using a poor pattern, is this inadvisable? According to official documentation, "The value can be of any type."
=>can only be used in array initializes, so$element = $value =>[];is meaningless.