更新 - 我修复了关键名称。这是我的新代码。如果我注释掉前一种类型的话,我已经让各种方法正常工作了。如果先前的排序运行,则以下排序将无法正常运行。为什么会发生这种情况,我该如何解决?
<?php
$poptable=array(
array('State' => 'Alabama',
'Capital' => 'Montgomery',
'pop2010' => 4779736,
'poprank' => 23),
array('State' => 'Alaska',
'Capital' => 'Juneau',
'pop2010' => 710231,
'poprank' => 47),
array('State' => 'Arizona',
'Capital' => 'Phoenix',
'pop2010' => 6329017,
'poprank' => 18),
array('State' => 'Arkansas',
'Capital' => 'Little Rock',
'pop2010' => 2915918,
'poprank' => 32),
array('State' => 'California',
'Capital' => 'Sacramento',
'pop2010' => 37253956,
'poprank' => 1),
array('State' => 'Colorado',
'Capital' => 'Denver',
'pop2010' => 5029196,
'poprank' => 22),
array('State' => 'Connecticut',
'Capital' => 'Hartford',
'pop2010' => 3518288,
'poprank' => 29),
array('State' => 'Delaware',
'Capital' => 'Dover',
'pop2010' => 897934,
'poprank' => 45),
array('State' => 'Florida',
'Capital' => 'Tallahassee',
'pop2010' => 18801301,
'poprank' => "4"),
array('State' => 'Georgia',
'Capital' => 'Atlanta',
'pop2010' => 9687653,
'poprank' => 9),
array('State' => 'Hawaii',
'Capital' => 'Boise',
'pop2010' => 1360301,
'poprank' => 42));
//Read column data into rows
foreach ($poptable as $key => $row)
{
$state[$key] = $row['State'];
$capital[$key] = $row['Capital'];
$pop2010[$key] = $row['pop2010'];
$poprank[$key] = $row['poprank'];
}
//foreach ($poptable as $row) {
// foreach ($row as $key => $value){
// ${$key}[] = $value;}}
echo "<pre>";
//Sort data by state name in ascending order
array_multisort($state, SORT_ASC, $poptable);
//Output sorted data
print_r($poptable);
echo "<hr>";
//Sort data by capital in descending order
array_multisort($capital, SORT_DESC, $poptable);
//Output sorted data
print_r($poptable);
echo "<hr>";
//Sort data by population in ascending order
array_multisort($pop2010, SORT_ASC, SORT_NUMERIC, $poptable);
//Output sorted data
print_r($poptable);
echo "<hr>";
//Sort data by population rank in descending order
array_multisort($poprank, SORT_DESC, SORT_NUMERIC, $poptable);
//OUTPUT sorted data
print_r($poptable);
echo "</pre>";
?>
我正在尝试对多维数组进行排序。但是array_multisort没有像我想象的那样工作。我必须使用array_multisort。我的前两种工作正常(州和资本分类)。没有正确排序的是pop2010和poprank键的数值。我已经尝试过将它们作为字符串,数字等。我对排序数值的输出似乎没有任何实际顺序。 这是我的阵列。
$poptable=array(
array('State' => 'Alabama',
'Capital' => 'Montgomery',
'pop2010' => 4779736,
'poprank' => "23"),
array('State' => 'Alaska',
'Capital' => 'Juneau',
'pop2010' => 710231,
'poprank' => "47"),
array('State' => 'Arizona',
'Capital' => 'Phoenix',
'pop2010' => 6329017,
'poprank' => "18"),
array('State' => 'Arkansas',
'Capital' => 'Little Rock',
'pop2010' => 2915918,
'poprank' => "32"),
array('State' => 'California',
'Capital' => 'Sacramento',
'pop2010' => 37253956,
'poprank' => "1"),
array('State' => 'Colorado',
'Capital' => 'Denver',
'pop2010' => 5029196,
'poprank' => "22"),
array('State' => 'Connecticut',
'Capital' => 'Hartford',
'pop2010' => 3518288,
'poprank' => "29"),
array('State' => 'Delaware',
'Capital' => 'Dover',
'pop2010' => 897934,
'poprank' => "45"),
array('State' => 'Florida',
'Capital' => 'Tallahassee',
'pop2010' => '18,801,301',
'poprank' => '4'),
array('State' => 'Georgia',
'Capital' => 'Atlanta',
'pop2010' => '9,687,653',
'poprank' => '9'),
array('State' => 'Hawaii',
'Capital' => 'Boise',
'pop2010' => '1,360,301',
'poprank' => '42'));
echo "<pre>"
//Sort data by state name in ascending order
array_multisort($State, SORT_ASC, $poptable);
//Output sorted data
print_r($poptable);
echo "<hr>";
//Sort data by capital in descending order
array_multisort($Capital, SORT_DESC, $poptable);
//Output sorted data
print_r($poptable);
echo "<hr>";
//Sort data by population
array_multisort($poprank, SORT_STRING, SORT_DESC, $poptable);
//Output sorted data
print_r($poptable);
echo "<hr>";
echo "</pre>";
?>
答案 0 :(得分:0)
试试这个
array_multisort($key1, SORT_ASC, $key2, SORT_DESC, $arrayname);