1

我有这个数组:

Array
(
    [0] => Array
        (
            [kw] => 46
            [anzahl_betten] => 100
        )

    [1] => Array
        (
            [kw] => 47
            [anzahl_betten] => 100
        )

    [2] => Array
        (
            [kw] => 45
            [anzahl_betten] => 100
        )

)

我想按“kw”顺序对其进行排序。然后我想通过foreach($array as $output) Array 和 kw 45 的 Array 应该是Array[0].

4

4 回答 4

2

为此使用usort()

//$array is your array
usort($array, function($x, $y)
{
   return $x['kw']<$y['kw']?-1:$x['kw']!=$y['kw'];
});
于 2013-11-07T09:34:16.407 回答
0
function subval_sort($a,$subkey) {
    $c = array();
    $b = array();
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    asort($b);
    foreach($b as $key=>$val) {
        $c[] = $a[$key];
    }
    return $c;
}

接着

$output = subval_sort($array_name,'kw'); 
于 2013-11-07T09:33:02.827 回答
0

也许这对你有用:

ksort($array);
于 2013-11-07T09:36:06.180 回答
0
    $a=array(array('kw'=>46,'anzahl_betten'=>100),array('kw'=>47,'anzahl_betten'=>100),array('kw'=>45,'anzahl_betten'=>100));
sort($a);
foreach($a as $x=>$x_value)
    {
print_r($x_value);  

   }
于 2013-11-07T10:07:06.227 回答