Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将国家语言名称转换为ISO代码首字母_Php_Localization_Iso - Fatal编程技术网

使用PHP将国家语言名称转换为ISO代码首字母

使用PHP将国家语言名称转换为ISO代码首字母,php,localization,iso,Php,Localization,Iso,对于jobs表,我正在寻找一种从某个国家获得ISO代码首字母的方法。 我有全名,但我想要两个字符的名字 例如: “美国”变成“美国”,或“荷兰”变成“荷兰” 我的代码是: $locations = array(); $args = array ( 'taxonomy' => 'location', 'hide_empty' => true, 'orderby' => 'name', 'order' => 'ASC', ); $get_l

对于jobs表,我正在寻找一种从某个国家获得ISO代码首字母的方法。
我有全名,但我想要两个字符的名字

例如:
“美国”变成“美国”,或“荷兰”变成“荷兰”

我的代码是:

$locations = array();

$args = array (
    'taxonomy' => 'location',
    'hide_empty' => true,
    'orderby' => 'name',
    'order' => 'ASC',
);

$get_locations = get_terms($args);

foreach ($get_locations as $key => $location) {
    $jobs_array = array();
    $location_name = $location->name;
    
    // Get jobs
    $post_type = 'jobs';
    $posts_per_page = -1;
    $jobs_args = array(
        'post_type' => $post_type,
        'post_status' => 'publish',
        'posts_per_page' => $posts_per_page,
        'location' => $location_slug,
    );
    
    $jobs = new WP_Query( $jobs_args );
    
    if($jobs->have_posts()) :
        while ( $jobs->have_posts() ): $jobs->the_post();
        // $jobs_array = $post;
        $jobs_array[] = $post->post_title;
        endwhile; wp_reset_postdata();
    endif;

    
    $locations[$location_slug] = $jobs_array;
}
结果是什么:

$locations = array();

$args = array (
    'taxonomy' => 'location',
    'hide_empty' => true,
    'orderby' => 'name',
    'order' => 'ASC',
);

$get_locations = get_terms($args);

foreach ($get_locations as $key => $location) {
    $jobs_array = array();
    $location_name = $location->name;
    
    // Get jobs
    $post_type = 'jobs';
    $posts_per_page = -1;
    $jobs_args = array(
        'post_type' => $post_type,
        'post_status' => 'publish',
        'posts_per_page' => $posts_per_page,
        'location' => $location_slug,
    );
    
    $jobs = new WP_Query( $jobs_args );
    
    if($jobs->have_posts()) :
        while ( $jobs->have_posts() ): $jobs->the_post();
        // $jobs_array = $post;
        $jobs_array[] = $post->post_title;
        endwhile; wp_reset_postdata();
    endif;

    
    $locations[$location_slug] = $jobs_array;
}

如您所见,键是全名。
我希望有人知道如何做到这一点。谢谢


这个问题也发布在WordPress Stack Exchange上,因为它使用WordPress帖子中的查询循环。

如果我没有正确理解所有内容,您希望数组键包含国家ISO代码而不是全名,对吗

我建议创建一个自定义类,它包含一个带有国家名称和ISO代码的数组。大概是这样的:

<?php

class CountryIsoCodes
{
    const COUNTRIES = [
    'AF' => 'Afghanistan',
    'AX' => 'Aland Islands',
    'AL' => 'Albania',
    'DZ' => 'Algeria',
    'AS' => 'American Samoa',
    'AD' => 'Andorra',
    'AO' => 'Angola',
    'AI' => 'Anguilla',
    'AQ' => 'Antarctica',
    'AG' => 'Antigua and Barbuda',
    'AR' => 'Argentina',
    'AM' => 'Armenia',
    'AW' => 'Aruba',
    'AU' => 'Australia',
    'AT' => 'Austria',
    'AZ' => 'Azerbaijan',
    'BS' => 'Bahamas the',
    'BH' => 'Bahrain',
    'BD' => 'Bangladesh',
    'BB' => 'Barbados',
    'BY' => 'Belarus',
    'BE' => 'Belgium',
    'BZ' => 'Belize',
    'BJ' => 'Benin',
    'BM' => 'Bermuda',
    'BT' => 'Bhutan',
    'BO' => 'Bolivia',
    'BA' => 'Bosnia and Herzegovina',
    'BW' => 'Botswana',
    'BV' => 'Bouvet Island (Bouvetoya)',
    'BR' => 'Brazil',
    'IO' => 'British Indian Ocean Territory (Chagos Archipelago)',
    'VG' => 'British Virgin Islands',
    'BN' => 'Brunei Darussalam',
    'BG' => 'Bulgaria',
    'BF' => 'Burkina Faso',
    'BI' => 'Burundi',
    'KH' => 'Cambodia',
    'CM' => 'Cameroon',
    'CA' => 'Canada',
    'CV' => 'Cape Verde',
    'KY' => 'Cayman Islands',
    'CF' => 'Central African Republic',
    'TD' => 'Chad',
    'CL' => 'Chile',
    'CN' => 'China',
    'CX' => 'Christmas Island',
    'CC' => 'Cocos (Keeling) Islands',
    'CO' => 'Colombia',
    'KM' => 'Comoros the',
    'CD' => 'Congo',
    'CG' => 'Congo the',
    'CK' => 'Cook Islands',
    'CR' => 'Costa Rica',
    'CI' => 'Cote d\'Ivoire',
    'HR' => 'Croatia',
    'CU' => 'Cuba',
    'CY' => 'Cyprus',
    'CZ' => 'Czech Republic',
    'DK' => 'Denmark',
    'DJ' => 'Djibouti',
    'DM' => 'Dominica',
    'DO' => 'Dominican Republic',
    'EC' => 'Ecuador',
    'EG' => 'Egypt',
    'SV' => 'El Salvador',
    'GQ' => 'Equatorial Guinea',
    'ER' => 'Eritrea',
    'EE' => 'Estonia',
    'ET' => 'Ethiopia',
    'FO' => 'Faroe Islands',
    'FK' => 'Falkland Islands (Malvinas)',
    'FJ' => 'Fiji the Fiji Islands',
    'FI' => 'Finland',
    'FR' => 'France, French Republic',
    'GF' => 'French Guiana',
    'PF' => 'French Polynesia',
    'TF' => 'French Southern Territories',
    'GA' => 'Gabon',
    'GM' => 'Gambia the',
    'GE' => 'Georgia',
    'DE' => 'Germany',
    'GH' => 'Ghana',
    'GI' => 'Gibraltar',
    'GR' => 'Greece',
    'GL' => 'Greenland',
    'GD' => 'Grenada',
    'GP' => 'Guadeloupe',
    'GU' => 'Guam',
    'GT' => 'Guatemala',
    'GG' => 'Guernsey',
    'GN' => 'Guinea',
    'GW' => 'Guinea-Bissau',
    'GY' => 'Guyana',
    'HT' => 'Haiti',
    'HM' => 'Heard Island and McDonald Islands',
    'VA' => 'Holy See (Vatican City State)',
    'HN' => 'Honduras',
    'HK' => 'Hong Kong',
    'HU' => 'Hungary',
    'IS' => 'Iceland',
    'IN' => 'India',
    'ID' => 'Indonesia',
    'IR' => 'Iran',
    'IQ' => 'Iraq',
    'IE' => 'Ireland',
    'IM' => 'Isle of Man',
    'IL' => 'Israel',
    'IT' => 'Italy',
    'JM' => 'Jamaica',
    'JP' => 'Japan',
    'JE' => 'Jersey',
    'JO' => 'Jordan',
    'KZ' => 'Kazakhstan',
    'KE' => 'Kenya',
    'KI' => 'Kiribati',
    'KP' => 'Korea',
    'KR' => 'Korea',
    'KW' => 'Kuwait',
    'KG' => 'Kyrgyz Republic',
    'LA' => 'Lao',
    'LV' => 'Latvia',
    'LB' => 'Lebanon',
    'LS' => 'Lesotho',
    'LR' => 'Liberia',
    'LY' => 'Libyan Arab Jamahiriya',
    'LI' => 'Liechtenstein',
    'LT' => 'Lithuania',
    'LU' => 'Luxembourg',
    'MO' => 'Macao',
    'MK' => 'Macedonia',
    'MG' => 'Madagascar',
    'MW' => 'Malawi',
    'MY' => 'Malaysia',
    'MV' => 'Maldives',
    'ML' => 'Mali',
    'MT' => 'Malta',
    'MH' => 'Marshall Islands',
    'MQ' => 'Martinique',
    'MR' => 'Mauritania',
    'MU' => 'Mauritius',
    'YT' => 'Mayotte',
    'MX' => 'Mexico',
    'FM' => 'Micronesia',
    'MD' => 'Moldova',
    'MC' => 'Monaco',
    'MN' => 'Mongolia',
    'ME' => 'Montenegro',
    'MS' => 'Montserrat',
    'MA' => 'Morocco',
    'MZ' => 'Mozambique',
    'MM' => 'Myanmar',
    'NA' => 'Namibia',
    'NR' => 'Nauru',
    'NP' => 'Nepal',
    'AN' => 'Netherlands Antilles',
    'NL' => 'Netherlands the',
    'NC' => 'New Caledonia',
    'NZ' => 'New Zealand',
    'NI' => 'Nicaragua',
    'NE' => 'Niger',
    'NG' => 'Nigeria',
    'NU' => 'Niue',
    'NF' => 'Norfolk Island',
    'MP' => 'Northern Mariana Islands',
    'NO' => 'Norway',
    'OM' => 'Oman',
    'PK' => 'Pakistan',
    'PW' => 'Palau',
    'PS' => 'Palestinian Territory',
    'PA' => 'Panama',
    'PG' => 'Papua New Guinea',
    'PY' => 'Paraguay',
    'PE' => 'Peru',
    'PH' => 'Philippines',
    'PN' => 'Pitcairn Islands',
    'PL' => 'Poland',
    'PT' => 'Portugal, Portuguese Republic',
    'PR' => 'Puerto Rico',
    'QA' => 'Qatar',
    'RE' => 'Reunion',
    'RO' => 'Romania',
    'RU' => 'Russian Federation',
    'RW' => 'Rwanda',
    'BL' => 'Saint Barthelemy',
    'SH' => 'Saint Helena',
    'KN' => 'Saint Kitts and Nevis',
    'LC' => 'Saint Lucia',
    'MF' => 'Saint Martin',
    'PM' => 'Saint Pierre and Miquelon',
    'VC' => 'Saint Vincent and the Grenadines',
    'WS' => 'Samoa',
    'SM' => 'San Marino',
    'ST' => 'Sao Tome and Principe',
    'SA' => 'Saudi Arabia',
    'SN' => 'Senegal',
    'RS' => 'Serbia',
    'SC' => 'Seychelles',
    'SL' => 'Sierra Leone',
    'SG' => 'Singapore',
    'SK' => 'Slovakia (Slovak Republic)',
    'SI' => 'Slovenia',
    'SB' => 'Solomon Islands',
    'SO' => 'Somalia, Somali Republic',
    'ZA' => 'South Africa',
    'GS' => 'South Georgia and the South Sandwich Islands',
    'ES' => 'Spain',
    'LK' => 'Sri Lanka',
    'SD' => 'Sudan',
    'SR' => 'Suriname',
    'SJ' => 'Svalbard & Jan Mayen Islands',
    'SZ' => 'Swaziland',
    'SE' => 'Sweden',
    'CH' => 'Switzerland, Swiss Confederation',
    'SY' => 'Syrian Arab Republic',
    'TW' => 'Taiwan',
    'TJ' => 'Tajikistan',
    'TZ' => 'Tanzania',
    'TH' => 'Thailand',
    'TL' => 'Timor-Leste',
    'TG' => 'Togo',
    'TK' => 'Tokelau',
    'TO' => 'Tonga',
    'TT' => 'Trinidad and Tobago',
    'TN' => 'Tunisia',
    'TR' => 'Turkey',
    'TM' => 'Turkmenistan',
    'TC' => 'Turks and Caicos Islands',
    'TV' => 'Tuvalu',
    'UG' => 'Uganda',
    'UA' => 'Ukraine',
    'AE' => 'United Arab Emirates',
    'GB' => 'United Kingdom',
    'US' => 'United States of America',
    'UM' => 'United States Minor Outlying Islands',
    'VI' => 'United States Virgin Islands',
    'UY' => 'Uruguay, Eastern Republic of',
    'UZ' => 'Uzbekistan',
    'VU' => 'Vanuatu',
    'VE' => 'Venezuela',
    'VN' => 'Vietnam',
    'WF' => 'Wallis and Futuna',
    'EH' => 'Western Sahara',
    'YE' => 'Yemen',
    'ZM' => 'Zambia',
    'ZW' => 'Zimbabwe'
];

    /**
     * @param string $countryName
     *
     * @return string
     */
    public function getIsoCodeByCountryName($countryName)
    {
        if (!isset(array_flip(self::COUNTRIES)[$countryName])) {
            trigger_error('Could not find country code ' . $countryName, E_USER_ERROR);
        }
    
        return array_flip(self::COUNTRIES)[$countryName];
    }
}
希望能有帮助。干杯

另外,如果找不到国家名称,我已经在类中添加了一个错误触发器。如果缺少国家,或者您使用的是不同的名称,您可以使用国家来丰富国家常数


请检查并调整国家名称,因为它们可能与您存储在DB中的国家名称有微小差异。。。确保它们匹配;)

查看这篇帖子,@Milad,我知道。我想用另一种方法来做。嗨@alieni,这确实是一种方法,但我将国家作为分类法导入,其中键是带破折号的名称。我通过在分类法中添加一个额外的字段来修复它。无论如何谢谢你!