Forum : I want to invert my array, can I do this?
Brief description  about Online courses   join in Online courses
View Balu  Prasad 's Profile

I want to invert my array, can I do this?

I want to invert my array, can I do this??????
Asked by Balu Prasad | Mar 17, 2010 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Balu,
The answer is yes, but it depends what you mean by invert. On clarification you wanted to amend the array so that the values and keys switched over, and this is done through using the function array_flip, like this:
<?php
$values = array("Fred","Bob","George");
print_r($values);

$values = array_flip($values);
print_r($values);
?>

Which returns:

Array
(
[0] => Fred
[1] => Bob
[2] => George
)
Array
(
[Fred] => 0
[Bob] => 1
[George] => 2
)
Mar 17, 2010