I often need the functionality to convert an array to an associative array (often in order to be able to check for existance of an entry using isset).
Let me give an example:
$test = array("foo", "bar", "faz");
I’d like to convert this to something like:
$test = array("foo" => true, "bar" => true, "faz" => true);
I know of these techniques which (almost) achieve what I want to do, but I’m searching for something slick and more elegant than this:
$new = array();
foreach ($test as $v) $new[$v] = true; // want to do it without a loop
$new = array_flip($test); // works for isset but array_values($new) are all different
$new = array_map(function() { return true; }, array_flip($test)); // would work but verbose
Any ideas?
1 Answer