-6

How I get first and second element in this array?

enter image description here

for example the first "AF" and second "Afghanistan", I need to put the value in html select tag

Reint
  • 155
  • 1
  • 4
  • 11

3 Answers3

0

Use array_slice:

$input_array = array('a'=>'a', 'b'=>'b', 'c'=>'c', 'd', 'e');
$first_two =  array_slice($input_array, 0, 2, true);
Giedrius Kiršys
  • 5,154
  • 2
  • 20
  • 28
0

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
JKidd404
  • 88
  • 6
0

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);
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96