如何排序多维数组?

时间:2015-03-08 06:52:52

标签: php arrays sorting multidimensional-array

我有一个多维PHP数组,每个数组都有一个子数组,其中包含状态列表,其大写字母,总体和人口等级。例如,多维数组看起来像

 $state_info = array(
'States' => array('utah','.....')
'Capitals' =>  array('Dallas','.....'),                 
'Population' => array(4779736,.....),
 'Ranks' => array(23,....)
);

我的目标是通过升序,然后降序来对列进行排序,我无法使用任何排序函数来处理底层代码。

foreach($state_info as $key => $row )
{
    if($key == "States"){
        echo "<br>Sorting first column: ....<br>";
        foreach($row as $values){
            sort($values);
            echo "<br>".$values;
        }

    }
    else if($key == "Capitals"){
        echo "<br><br>Sorting second column: ....<br>";
        foreach($row as $values){
            sort($values);
            echo "<br>".$values;
        }
    }
    else if($key == "Population"){
        echo "<br><br>Sorting third column: ....<br>";
        foreach($row as $values){
            //sort values here
            sort($values);
            echo "<br>".$values;
        }
    }
    else if($key == "Ranks"){
        echo "<br><br>Sorting fourth column: ....<br>";
        foreach($row as $values){
            sort($values);
            echo "<br>".$values;
        }
    }

}

5 个答案:

答案 0 :(得分:0)

您也可以考虑使用switch语句

$state_info = array(
'States' => array('utah','zibra','aba'),
'Capitals' =>  array('Dallas','Zbcap','Abcap'),                 
'Population' => array(4779736,9090,2030),
 'Ranks' => array(23,24,2)
);

foreach($state_info as $key => $row )
{
    if($key == "States"){
        echo "<br>Sorting first column: ....<br>";

    sort($row);
    foreach($row as $values){
        echo "<br>".$values;
    }

}
else if($key == "Capitals"){
    echo "<br><br>Sorting second column: ....<br>";

    sort($row);
    foreach($row as $values){
        echo "<br>".$values;
    }
}
else if($key == "Population"){
    echo "<br><br>Sorting third column: ....<br>";

    //sort values here
    sort($row);
    foreach($row as $values){
        echo "<br>".$values;
    }
}
else if($key == "Ranks"){
    echo "<br><br>Sorting fourth column: ....<br>";

    sort($row);
    foreach($row as $values){
        echo "<br>".$values;
    }
}

}

答案 1 :(得分:0)

将数组初始化为

$states = array();
$Capitals= array();
$Population= array();
$Ranks= array();

当您启动foreach($ arr as $ key =&gt; $ data)循环时,您必须开始推送

array_push($states,$state_info['States']);
array_push($Capitals,$state_info['Capitals']);
array_push($Population,$state_info['Population']);
array_push($Ranks,$state_info['Ranks']);

这里$ key很重要,如果你想获取第5个数组元素然后你必须跳转键,如$ states [5],$ Capitals [5],$ Ranks [5],$ Population [5]

在排序任何上述数组时,保持它们的索引(不要在键上使用任何排序)......

这可能会让你想到做得更好......

答案 2 :(得分:0)

  

我发布了这个js解决方案,忽略了这是一个php问题。我去删除帖子,但注意到OP评论了关于将数据引入表格的另一个答案。这个答案可能对客户端的排序很有用。

使用它给它你的数组,你要排序的键的名称和一个真/假布尔值来选择排序顺序。上升或下降 它适用于数字或字符串

var sortArray = function(array, key, desc) {
    //sortArray(array, string, boolean);
    //sort array by specific key, asc or desc - only handles strings, numbers
    if(desc == undefined) desc = false; //default to false if not set
    if(isNumeric(array[0][key])){
        //sort by number
        return array.sort(function(a, b) {
            if(!desc){
                return a[key] - b[key]; //sorts asc
            }else{
                return b[key] - a[key]; //sorts desc
            }
        });
    }else if(typeof array[0][key] == "string"){
        //sort by alpha;
        return array.sort(function(a, b){
            if(!desc){
                return a[key] < b[key] ? -1 : 1; //sorts asc
            }else{
                return a[key] < b[key] ? 1 : -1; //sorts desc
            }
        });
    }else{
        console.log("type unknown, no sorting done");
        return array;
    }
}

var isNumeric = function(num) {
  return !isNaN(parseFloat(num)) && isFinite(num);
}

答案 3 :(得分:0)

单向排序

foreach($state_info as $key)
{
    switch ($key) {
        case 'States':
            arsort($state_info['States']); // high to low
            foreach ($state_info['States'] as $key2 => $value) echo $value;
            break;
        case 'Capitals':
            arsort($state_info['Capitals']); // high to low
            foreach ($state_info['Capitals'] as $key2 => $value) echo $value;
            break;
        case 'Population':
            arsort($state_info['Population']); // high to low
            foreach ($state_info['Population'] as $key2 => $value) echo $value;
            break;
        case 'Ranks':
            arsort($state_info['Ranks']); // high to low
            foreach ($state_info['Ranks'] as $key2 => $value) echo $value;
            break;
    }
}

答案 4 :(得分:0)

<?php

$state_info = array(
'States' => array('Utah','AAA', 'XXX'),
'Capitals' =>  array('Dallas','BBB', 'ZZZ'),                 
'Population' => array(4779736, 1111111, 222222),
 'Ranks' => array(23, 12, 1)
);

function printA() {
global $asc;

foreach($asc as $key => $value) {
  echo $key . ': <br/>';
  foreach($value as $info) {
     echo $info . '<br>'; 
  }   
  echo '<br>';
}   
}   

$asc = array_map(function($element) {sort($element); return($element);}, $state_info);

echo '<p><b>Asc</b></p>';
printA();

$asc = array_map(function($element) {rsort($element); return($element);}, $state_info);

echo '<p><b>Desc</b></p>';
printA();
?>