首页 > 解决方案 > 为什么我不能对这个数组进行排序?

问题描述

我从键值格式的分类术语中获得了这个数组,以填充表单元素选择选项。

除英语以外的其他语言的顺序不正确,因此我尝试对其进行排序但没有结果。

这是数组:

Array
(
    [AT] => Autriche
    [BE] => Belgique
    [BG] => Bulgarie
    [HR] => Croatie
    [CY] => Chypre
    [CZ] => République Tchèque
    [DK] => Danemark
)

我尝试 asort() 但它返回“1”。

有人有线索吗?

编辑:

使用的代码:

foreach ($tree as $term) {
  $term_object_wrapper = entity_metadata_wrapper('taxonomy_term', $term_object = taxonomy_term_load($term->tid));

  if ($term_object_wrapper->field_member_state->value() == 1) {
    $iso_code = $term_object_wrapper->iso_3166_1_alpha_2_code->value();
    $i18ned_country_term = i18n_taxonomy_localize_terms($term_object);
    $output_localized[$iso_code]  = $i18ned_country_term->name;
  }
}
$foo = asort($output_localized);
print('<pre>');
print_r( $foo );
print('</pre>');

结果为“1”

标签: phparrayssorting

解决方案


  <?php
  $array = [
      "AT" => "Test1",
      "BE" => "Test2",
      "BG" => "Test3",
      "HR" => "Test4",
      "CY" => "Test5",
      "CZ" => "Test6",
      "DK" => "Test7"
  ];

  asort($array);

  echo "<pre>";
  print_r($array);
  ?>

输出:

  Array
  (
      [AT] => Test1
      [BE] => Test2
      [BG] => Test3
      [HR] => Test4
      [CY] => Test5
      [CZ] => Test6
      [DK] => Test7
  )

请检查并确认

谢谢

穆图


推荐阅读