I have an array like this
array(
0 => "bar",
1 => "foo",
);
and I want output like this
array(
"bar" => "bar",
"foo" => "foo",
);
how can i achieve this?
Assuming your original array is strictly a numeric array:
Actually works for non-numeric as well:
array_combine($array, $array);
$arr = array(
0 => "bar",
1 => "foo",
);
$arrCombine = array_combine($arr, $arr);
print_r($arrCombine);
gives Array ( [bar] => bar [foo] => foo )
in case non numeric array
simple logic of using value as key works
$array1 = array(
0 => "bar",
"cat" => "foo",);
foreach($array1 as $key => $value)
{
$array1[$value] = $value;
unset($array1[$key]);
}
var_dump($array1);
gives
array(2) {
["bar"]=>
string(3) "bar"
["foo"]=>
string(3) "foo"
}
and if its strict use answer above, would be faster i guess and clean code too.