issue with arrays echo out locales

Viewed 28

I am trying to echo out the results from the array in a foreach for location however running into problems with the array.

<select name="locale">
<?php
foreach ($locales at $location => $lo) {
    echo '<option value="' . $lo[0] . '">' . $lo[1] . '</option>';
}
?>
</select> 

The array looks like the following;

$locales = array(
    'aa_DJ' => 'Afar (Djibouti)',
    'aa_ER' => 'Afar (Eritrea)',
    'aa_ET' => 'Afar (Ethiopia)',
    'af_ZA' => 'Afrikaans (South Africa)',
    'sq_AL' => 'Albanian (Albania)',
    'sq_MK' => 'Albanian (Macedonia)',
    'am_ET' => 'Amharic (Ethiopia)',
    'ar_DZ' => 'Arabic (Algeria)',
    'ar_BH' => 'Arabic (Bahrain)',
    'ar_EG' => 'Arabic (Egypt)',
    'ar_IN' => 'Arabic (India)',
    'ar_IQ' => 'Arabic (Iraq)',
    'ar_JO' => 'Arabic (Jordan)',
    'ar_KW' => 'Arabic (Kuwait)',
    'ar_LB' => 'Arabic (Lebanon)',
    'ar_LY' => 'Arabic (Libya)',
    'ar_MA' => 'Arabic (Morocco)',
    'ar_OM' => 'Arabic (Oman)',
    'ar_QA' => 'Arabic (Qatar)',
    'ar_SA' => 'Arabic (Saudi Arabia)',
    'ar_SD' => 'Arabic (Sudan)',
    'ar_SY' => 'Arabic (Syria)',
    'ar_TN' => 'Arabic (Tunisia)',
    'ar_AE' => 'Arabic (United Arab Emirates)',
    'ar_YE' => 'Arabic (Yemen)',
    'an_ES' => 'Aragonese (Spain)',
    'hy_AM' => 'Armenian (Armenia)',
    'as_IN' => 'Assamese (India)',
    'ast_ES' => 'Asturian (Spain)',
    'az_AZ' => 'Azerbaijani (Azerbaijan)',
    'az_TR' => 'Azerbaijani (Turkey)',
    'eu_FR' => 'Basque (France)',
    'eu_ES' => 'Basque (Spain)',
    'be_BY' => 'Belarusian (Belarus)',
    'bem_ZM' => 'Bemba (Zambia)',
    'bn_BD' => 'Bengali (Bangladesh)',
    'bn_IN' => 'Bengali (India)',
    'ber_DZ' => 'Berber (Algeria)',
    'ber_MA' => 'Berber (Morocco)',
    'byn_ER' => 'Blin (Eritrea)',
    'bs_BA' => 'Bosnian (Bosnia and Herzegovina)',
    'br_FR' => 'Breton (France)',
    'bg_BG' => 'Bulgarian (Bulgaria)',
    'my_MM' => 'Burmese (Myanmar [Burma])',
    'ca_AD' => 'Catalan (Andorra)',
    'ca_FR' => 'Catalan (France)',
    'ca_IT' => 'Catalan (Italy)',
    'ca_ES' => 'Catalan (Spain)',
    'zh_CN' => 'Chinese (China)',
    'zh_HK' => 'Chinese (Hong Kong SAR China)',
    'zh_SG' => 'Chinese (Singapore)',
    'zh_TW' => 'Chinese (Taiwan)',
    'cv_RU' => 'Chuvash (Russia)',
    'kw_GB' => 'Cornish (United Kingdom)',
    'crh_UA' => 'Crimean Turkish (Ukraine)',
    'hr_HR' => 'Croatian (Croatia)',
    'cs_CZ' => 'Czech (Czech Republic)',
    'da_DK' => 'Danish (Denmark)',
    'dv_MV' => 'Divehi (Maldives)',
    'nl_AW' => 'Dutch (Aruba)',
    'nl_BE' => 'Dutch (Belgium)',
    'nl_NL' => 'Dutch (Netherlands)',
    'dz_BT' => 'Dzongkha (Bhutan)',
    'en_AG' => 'English (Antigua and Barbuda)',
    'en_AU' => 'English (Australia)',
    'en_BW' => 'English (Botswana)',
    'en_CA' => 'English (Canada)',
    'en_DK' => 'English (Denmark)',
    'en_HK' => 'English (Hong Kong SAR China)',
    'en_IN' => 'English (India)',
    'en_IE' => 'English (Ireland)',
    'en_NZ' => 'English (New Zealand)',
    'en_NG' => 'English (Nigeria)',
    'en_PH' => 'English (Philippines)',
    'en_SG' => 'English (Singapore)',
    'en_ZA' => 'English (South Africa)',
    'en_GB' => 'English (United Kingdom)',
    'en_US' => 'English (United States)',
    'en_ZM' => 'English (Zambia)',
    'en_ZW' => 'English (Zimbabwe)',
    'eo' => 'Esperanto',
    'et_EE' => 'Estonian (Estonia)',
    'fo_FO' => 'Faroese (Faroe Islands)',
    'fil_PH' => 'Filipino (Philippines)',
...

I am trying to get the location code in to the value and the easy to read location in the displayed option. Just not having much luck.

Many Thanks :-)

2 Answers

$lo[0] does not hold the locale. That's being stored in $lo and the location is already in $location by the definition of the foreach loop.

<select name="locale">
<?php
foreach ($locales at $lo => $location) {
    echo '<option value="' . $lo . '">' . $location . '</option>';
}
?>
</select> 

$lo is not an array, it's a string like 'Afar (Djibouti)'. $lo[0] is the first character, $lo[1] is the second character.

You want to use $location and $lo in your options. When you write foreach ($locales as $location => $lo), the array keys go in $location, and the corresponding values go in $lo.

Also you have a typo: at should be as.

foreach ($locales as $location => $lo) {
    echo '<option value="' . $location . '">' . $lo . '</option>';
}
Related