开发者

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

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?

Solution

I realized that this is not possible. When you开发者_JS百科 try to set a value in an array with a duplicate key, it overwrites the first key. I came up with a different solution, but have accepted the highest rated answer.


Instead of using a one dimensional array as id=> value, you can use a two dimensional array, such as

$countries = array(
    0 => array(
                   'country_id' => 47,
                   'country' => 'USA'
               ),
    1 => array(
                   'country_id' => 39,
                   'country' => 'Canada'
               ),
    2 => array(
                   'country_id' => 1,
                   'country' => 'Afghanistan'
               ),
    ......
);


You can manually add them in html. You could copy and paste the loop sending it the 2 element array. You could make the loop into a function and call it using the 2 element array and then the longer array. You could generate the array as [0..inf] => Array($key, $value) then get the key value by using list($key, $val) = $arr[x], making you able to manually add USA and canada without a problem.


Since you explicitly want duplicates, you could just use an array and not an associative array.

array(
    [0] => " -- Select -- "
    [1] => array(name: "Afghanistan", code: 1)
    [2] => "array(name: Albania", code: 3)
)

and so on, or may create a Country object and have an array of those.

class Country {
    public $name;
    public $code;
    ..
}

$countries[] = new Country('USA', 47);
$countries[] = new Country('Canada', 39);
$countries[] = new Country('Afghanistan', 1);
...


For purposes of usability, it's really not a good to list countries twice in a select menu. It's kind of confusing.

But if you have your heart set on it, why not loop through the associative array with this:

$top_countries = array(
    [0] = "USA";
    [1] = "Canada";
)

and then

foreach($top_countries as $top_of_list) {
    foreach($list_of_countries as $this_country) {
        if($this_country == $top_of_list) {
            $stored_string .= // Select HTML formatting with $this_country;
        }
    }
}

// Pointer reset and rest of the code 
// to add rest of the countries.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜