如何以特定方式在PHP中对数组进行排序

时间:2015-03-27 10:10:23

标签: php arrays sorting multidimensional-array foreach

代码:

$array = array('even' => array(array('key' => 'test','key2' => 'Wn'), array('key' => 'test3', 'key2' => 'Bx')),
                'not_even' => array(array('key' => 'test2','key2' => 'Xn'), array('key' => 'test4', 'key2' => 'Gy')),
);

echo '<pre>';
print_r($array);
echo '</pre>';

输出:

Array
(
    [even] => Array
        (
            [0] => Array
                (
                    [key] => test
                    [key2] => Wn
                )

            [1] => Array
                (
                    [key] => test3
                    [key2] => Bx
                )

        )

    [not_even] => Array
        (
            [0] => Array
                (
                    [key] => test2
                    [key2] => Xn
                )

            [1] => Array
                (
                    [key] => test4
                    [key2] => Gy
                )

        )

)

我想要排序,结果应该是:

Array
(
    [0] => Array
        (
            [key] => test
            [key2] => Wn
        )

    [1] => Array
        (
            [key] => test2
            [key2] => Xn
        )

    [2] => Array
        (
            [key] => test3
            [key2] => Bx
        )

    [3] => Array
        (
            [key] => test4
            [key2] => Gy
        )

)

那怎么样呢?数组中的键应该是: 测试 TEST2 TEST3 TEST4。 如何使用foreach或其他东西呢?什么是最好的解决方案。 重要的是&#34; key&#34;,&#34; key2&#34;没关系。

3 个答案:

答案 0 :(得分:1)

试试这个

$array = array('even' => array(array('key' => 'test5','key2' => 'Wn'),    array('key' => 'test10', 'key2' => 'Bx')),
'not_even' => array(array('key' => 'test1','key2' => 'Xn'), array('key' => 'test', 'key2' => 'Gy')),
);

$new_array = array();

// changing  structure
foreach($array as $values){
$new_array = array_merge($new_array, $values);
}

// dump array
echo '<pre>';
print_r($new_array );
echo '</pre>';

// sorting
/// function for sorting
function cmp($a, $b)
{
   return strcasecmp($a['key'], $b['key']);
}

// sort by 'key' 
uksort($new_array, "cmp");

// dump array
echo '<pre>';
print_r($new_array );
echo '</pre>';

注意:这是字符串排序所以test10&lt; test5

答案 1 :(得分:0)

您可以使用array_multisort()

尝试这样的事情:

foreach ($array as $key => $row) {
   $array1[$key]  = $row[0]; 
}

array_multisort($array1, SORT_DESC, $array);

答案 2 :(得分:0)

试试这个例子,我已经在我的系统中进行了测试,并且它运行良好。

<?php
$array = array('even' => array(array('key' => 'test','key2' => 'Wn'), array('key' => 'test3', 'key2' => 'Bx')),
                 'not_even' => array(array('key' => 'test2','key2' => 'Xn'), array('key' => 'test4', 'key2' => 'Gy')),
);
//Before Sorting
echo '<pre>';
print_r($array);
echo '</pre>';

$my_array = array();
foreach($array as $values){
$my_array = array_merge($my_array, $values);
}

//After Sorting
asort($my_array);
echo '<pre>';
print_r($my_array );
echo '</pre>'; 
?> 

<强>输出: -

Array
(
    [even] => Array
        (
            [0] => Array
                (
                    [key] => test
                    [key2] => Wn
                )

            [1] => Array
                (
                    [key] => test3
                    [key2] => Bx
                )

        )

    [not_even] => Array
        (
            [0] => Array
                (
                    [key] => test2
                    [key2] => Xn
                )

            [1] => Array
                (
                    [key] => test4
                    [key2] => Gy
                )

        )

)

Array
(
    [0] => Array
        (
            [key] => test
            [key2] => Wn
        )

    [2] => Array
        (
            [key] => test2
            [key2] => Xn
        )

    [1] => Array
        (
            [key] => test3
            [key2] => Bx
        )

    [3] => Array
        (
            [key] => test4
            [key2] => Gy
        )

)

我认为这是你想要的。