1

私はこのような国の配列を持っています

array(249) {
  [0]=>
  array(4) {
    ["country_id"]=>
    string(1) "2"
    ["country_name_en"]=>
    string(19) "Åland Islands"
    ["country_alpha2"]=>
    string(2) "AX"
    ["country_alpha3"]=>
    string(3) "ALA"
  }
  etc.
}

最初の文字で分割したいので、このような配列を取得します

array(26) {
 'A' => array(10) {
    array(4) {
      ["country_id"]=>
      string(1) "2"
      ["country_name_en"]=>
      string(19) "Åland Islands"
      ["country_alpha2"]=>
      string(2) "AX"
      ["country_alpha3"]=>
      string(3) "ALA"
    }
    etc.
  }
  etc.
}

ただし、問題は、国名の配列に最初の文字としてhtmlエンティティが含まれていることです。

これを行う方法はありますか?

前もって感謝します

ピーター

4

2 に答える 2

2

Åland Islandsの下に提出したい場合は、すでに提案されているhtml_entity_decode()Aよりも少し多くのことを行う必要があります。

intlには、に変換する関数であるNormalizer::normalize()が含まれています。まだ混乱していますか?その Unicode シンボル (U+00C5) は、UTF-8 では(構成) および(分解)として表すことができます。は、です。ÅÅ0xC3850x41CC8A0x41A0xCC8Å

したがって、島を適切にファイルするには、次のようにする必要があります。

$string = "Åland Islands";
$s = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
$s = Normalizer::normalize($s, Normalizer::FORM_KD);
$s = mb_substr($s, 0, 1);

環境にintlがインストールされていない可能性があります。その場合は、文字列を英数字部分に縮小する関数であるurlify()を調べることができます。


上記であなたができるはずです

  1. 元の配列をループする
  2. 国名を抽出する
  3. 国名をサニタイズし、最初の文字を抽出する
  4. (3)の性質に基づいて新しい配列を構築する

注: 国ArmeniaAustriaおよび国Australiaはすべて の下にファイルされることに注意してくださいA

于 2012-06-23T15:08:36.600 に答える
1

配列をループし、html_entity_decode()を使用してhtmlエンティティをデコードしてから、mb_substr()を使用して分割します

foreach($array as $values) {
    $values['country_name_en'] = html_entity_decode($values['country_name_en']);
    $index = mb_substr($values['country_name_en'], 0, 1);

    $new_array[$index] = $values;
}

または、jlcdが提案する関数を使用できます。

function substr_unicode($str, $s, $l = null) {
    return join("", array_slice(
        preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $s, $l));
}

foreach($array as $values) {
    $values['country_name_en'] = html_entity_decode($values['country_name_en']);
    $index = substr_unicode($values['country_name_en'], 0, 1);

    $new_array[$index] = $values;
}
于 2012-06-23T14:12:52.873 に答える