通过第一键外部值的php阶多维数组

时间:2018-10-16 10:11:29

标签: php arrays

我需要按地区名称对多维数组重新排序。 但是我的多数组实际上是通过REGION_ID(第一个键)索引的,以使其像这样:

$array = array(
  1 => array(   // 1 is REGION_ID
    "Value" => "Val-1",
    "Children" => array(
       ...
    )
  ),
  2 => array(   // 2 is REGION_ID
    "Value" => "Val-2",
    "Children" => array(
       ...
    )
  ),
  3 => array(   // 3 is REGION_ID
    "Value" => "Val-3",
    "Children" => array(
       ...
    )
  ), 
  4 => array(   // 4 is REGION_ID
    "Value" => "Val-4",
    "Children" => array(
       ...
    )
  ),
  ...
);

foreach ( $array as $region_id => $sub_array ) {
  var_dump($region_id);
}

我的表REGION在哪里:

ID   |   Name
--------------
1    |  Emilia
--------------
2    |  Calabria
--------------
3    |  Veneto
--------------
4    |  Puglia
--------------
...

我想按表名对$ array重新排序: 然后是卡拉布里亚(Calabria),艾米利亚(Emilia),普利亚(Puglia),威尼托(Veneto)...所以我的$ array必须是:

$array = array(
      2 => array(   
        "Value" => "Val-2",
        "Children" => array(
           ...
        )
      ),
      1 => array(  
        "Value" => "Val-1",
        "Children" => array(
           ...
        )
      ),
      4 => array(   
        "Value" => "Val-4",
        "Children" => array(
           ...
        )
      ), 
      3 => array( 
        "Value" => "Val-3",
        "Children" => array(
           ...
        )
      ),
      ...
    );

我不知道如何使用我的逻辑进行排序...

2 个答案:

答案 0 :(得分:0)

将正确排序的数组从数据库分配给$ sorting

$ sorting = ... [某些函数以所需顺序从数据库中检索排序后的数组]

例如“ SELECT * FROM REGION ORDER BY Name

勘误表: 如果无法在SQL查询中添加“ ORDER BY”,则只需获取值并使用usort(http://php.net/manual/en/function.usort.php)函数可使$ sorting数组具有正确的顺序即可。

因此,您应该有类似(等效)的内容:

$sorting = [
    ['ID' => 2, 'Name' => 'Calabria'],
    ['ID' => 1, 'Name' => 'Emilia'],
    ['ID' => 4, 'Name' => 'Puglia'],
    ['ID' => 3, 'Name' => 'Veneto']
];

或var_dump($ sorting)

array (size=4)
  0 => 
    array (size=2)
      'ID' => int 2
      'Name' => string 'Calabria' (length=8)
  1 => 
    array (size=2)
      'ID' => int 1
      'Name' => string 'Emilia' (length=6)
  2 => 
    array (size=2)
      'ID' => int 4
      'Name' => string 'Puglia' (length=6)
  3 => 
    array (size=2)
      'ID' => int 3
      'Name' => string 'Veneto' (length=6)

现在运行以下循环:

$sorted_array = [];
foreach ($sorting as $sort_element) {
    $sorted_array[] = $array[$sort_element['ID']];
}

$ sorted_array将具有所需的排序顺序

array (size=4)
  0 => 
    array (size=2)
      'Value' => string 'Val-2' (length=5)
      'Children' => ...
  1 => 
    array (size=2)
      'Value' => string 'Val-1' (length=5)
      'Children' => ...
  2 => 
    array (size=2)
      'Value' => string 'Val-4' (length=5)
      'Children' => ...
  3 => 
    array (size=2)
      'Value' => string 'Val-3' (length=5)
      'Children' => ...

答案 1 :(得分:0)

<?php
$array = array(
  1 => array(   
      "Value" => "Val-1",
      "Children" => array(
     )
  ),
  2 => array(  
    "Value" => "Val-2",
    "Children" => array(
    )
  ),
  3 => array(   
    "Value" => "Val-3",
    "Children" => array(
    )
  ), 
  4 => array( 
    "Value" => "Val-4",
    "Children" => array(
    )
  ),
);

$region = array(1 => 'Emilia', 2 => 'Calabria', 3 => 'Veneto', 4 => 'Puglia');
asort($region);
$new_array = [];

foreach ($region as $key => $value) {
  $new_array[ $key ] = $array[ $key ];
}

print_r($new_array);

您可以在这里看到此方法:http://tpcg.io/K23FfH