i have a array of string elements like this:
$myArray = array(“apple”,”orange”,”pear”,”peach”);
if i select pear, i want the array to be searched
and if ‘pear’ is found, i want to move it from where
it is to the front of the array.
is there an easy way to do this?
i can detect that ‘pear’ exists using in_array(‘pear’,$my_array),
but can i simply change the key to zero to make it the first element?
or do i have to loop through the array to find it’s index and store it as a variable($found), then unset it, and then use unshift($my_array,$found);
wouldn’t reorganizing the key be faster since i’m not actually deleting the element.
if so how can i code it to simply change the key to move that element to the front of the array? i can do it with a loop but it seems like just changing the key would be much faster.