PHP: How to copy elements from an associative array and place them at the beginning of the array?

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2010-03-12T20:51:35Z Indexed on 2010/03/13 5:05 UTC
Read the original article Hit count: 322

Filed under:
|

I have an array of countries that I will be using in a select menu:

array(
    [0] => " -- Select -- "
    [1] => "Afghanistan"
    [3] => "Albania"
    [4] => "Algeria"
    [39] => "Canada"
    [47] => "USA"
)
//etc...

I want to copy create copies of the Canada and USA entries and place them at the front of my array. So the array should end up looking like this:

array(
    [0] => " -- Select -- "
    [47] => "USA"
    [39] => "Canada"
    [1] => "Afghanistan"
    [3] => "Albania"
    [4] => "Algeria"
    [39] => "Canada"
    [47] => "USA"
)
//etc...

The array keys correspond to their ID in the database, so I can't change the keys. How can I achieve this?

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays