Alex Rivera | Logout

Swap the position of two array values without preserving keys

Asked 2012-07-02T17:20:57.723
35

I have an array:

array(
    0 => 'contact',
    1 => 'home',
    2 => 'projects'
);

and I need to swap the 'contact' with 'home', so the array would be like:

array(
    0 => 'home',
    1 => 'contact',
    2 => 'projects'
);

How can I do this with PHP?

Edit
Report

1 Answer

47

Try this:

$a = array(
    0 => 'contact',
    1 => 'home',
    2 => 'projects'
);
$temp = $a[0];
$a[0] = $a[1];
$a[1] = $temp;
answered 2012-07-02T17:28:02.843

Your Answer