-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

5
  • 1
    What you have tried so far ..? Commented Apr 7, 2016 at 6:52
  • echo $arr[235]["AF"]; Commented Apr 7, 2016 at 6:53
  • unset[YOURARRAY['']]; Commented Apr 7, 2016 at 6:56
  • i do array_chunk, array_shift, array_values($countries) it return "htmlentities() expects parameter 1 to be string, array given" unfortunately the keys is not numeric, anyone help? Commented Apr 7, 2016 at 7:06
  • Post code, not images. This is not Flickr. Commented Apr 7, 2016 at 7:22

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

I need first and second element and it works, thanks you help my day :)
0

Use array_slice:

$input_array = array('a'=>'a', 'b'=>'b', 'c'=>'c', 'd', 'e');
$first_two =  array_slice($input_array, 0, 2, true);

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.