How I get first and second element in this array?
for example the first "AF" and second "Afghanistan", I need to put the value in html select tag
How I get first and second element in this array?
for example the first "AF" and second "Afghanistan", I need to put the value in html select tag
Use array_slice
:
$input_array = array('a'=>'a', 'b'=>'b', 'c'=>'c', 'd', 'e');
$first_two = array_slice($input_array, 0, 2, true);
From your code it looks like you actually want the key and the value...
<select name="country">
<?php
foreach($countryArray as $code=>$country){
echo "<option value=".$code.">".$country."</option>";
}
</select
You could use array_slice
$arrays = [
"" => "Select country",
"AF" => "Afghanistan",
"DZ" => "Algerian",
"AO" => "Angola"
];
$first = array_slice($arrays, 0, 1);
$second = array_slice($arrays, 1, 1);
$first_two = array_slice($arrays, 0, 2, true);
$first_three = array_slice($arrays, 0, 3, true);