Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What would be the best method of moving any element of an associative array to the beginning of the array?

For example, say I have the following array:

$myArray = array(
    'two'   => 'Blah Blah Blah 2',
    'three' => 'Blah Blah Blah 3',
    'one'   => 'Blah Blah Blah 1',
    'four'  => 'Blah Blah Blah 4',
    'five'  => 'Blah Blah Blah 5',
);

What i want to do is move the 'one' element to the beginning and end up with the following array:

$myArray = array(
    'one'   => 'Blah Blah Blah 1',
    'two'   => 'Blah Blah Blah 2',
    'three' => 'Blah Blah Blah 3',
    'four'  => 'Blah Blah Blah 4',
    'five'  => 'Blah Blah Blah 5',
);
share|improve this question

4 Answers 4

up vote 22 down vote accepted

This works:

$myArray = array('one' => $myArray['one']) + $myArray;
share|improve this answer
    
That does indeed work! Thank you. –  user1493356 Jun 30 '12 at 18:04
    
Any idea what the big-O performance of this is? –  andrewtweber Jul 1 '12 at 15:50
2  
@andrewtweber No, I don't, but I did some testing and found that, compared to Emil's approach, it is 3x faster and takes half as much memory. I also found that it takes 20% more time than moving the same element to the end of the array. –  Mark Eirich Jul 1 '12 at 22:17

If you have numerical array keys and want to reindex array keys, it would be better to put it into array_merge like this:

$myArray = array_merge(array($key => $value) + $myArray );
share|improve this answer

A bit late, but in case anyone needs it, I created this little snippet.

function arr_push_pos($key, $value, $pos, $arr) 
{
    $new_arr = array();
    $i = 1;

    foreach ($arr as $arr_key => $arr_value) 
    {
        if($i == $pos) 
            $new_arr[$key] = $value;

        $new_arr[$arr_key] = $arr_value;

        ++$i;
    }

    return $new_arr;
}

Just adjust it to suit your needs, or use it and unset the index to move. Works with associative arrays too.

share|improve this answer

There's a function in the comments of the PHP manual for array_unshift which can be used to add an element, with key, to the beginning of an array:

function array_unshift_assoc(&$arr, $key, $val)
{
    $arr = array_reverse($arr, true);
    $arr[$key] = $val;
    return array_reverse($arr, true);
}

Unset the element and reinsert it again with the above function:

$tmp = $myArray['one'];
unset($myArray['one']);
$myArray = array_unshift_assoc($myArray, 'one', $tmp);

A more general approach may be to use uksort to sort your array by keys and provide a sorting function of your own.

share|improve this answer
    
Hi - thanks for the answer, though i dont want to sort the array in any way. I want to be able to know an elements key and move only that specfic element to the beginning of the array –  user1493356 Jun 30 '12 at 18:01
    
I think the "=" is a syntax error: return = array_reverse($arr, true); –  Mark Eirich Jun 30 '12 at 18:10
    
Also, when I run your code I get an array with only one element in it... :-( –  Mark Eirich Jun 30 '12 at 18:11
    
Found and fixed the errors. This code now runs. –  Mark Eirich Jun 30 '12 at 18:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.