PHP array_multisort没有按预期排序我的多维数组

时间:2013-06-26 15:47:30

标签: php arrays multidimensional-array

我正在尝试使用array_multisort函数

对多维数组进行排序

我期待多维数组使用$ sort中的值来自我排序。 $ sort与我的多维数组的volume元素有关。

我希望name元素按此顺序排列:

'name'=> 8,'name'=> 6,'name'=> 7,'name'=> 9','name'=> 10,'name'=> 10,

退回的订单不符合预期。也许我误解了array_multisort是如何工作的?

Array
(
[test1] => Array
    (
        [volume] => 67
        [edition] => 2
        [name] => 6
        [num] => 2
    )

[test2] => Array
    (
        [volume] => 86
        [edition] => 1
        [name] => 7
        [type] => 2
    )

[test3] => Array
    (
        [volume] => 85
        [edition] => 6
        [name] => 8
        [type] => 2
    )

[test4] => Array
    (
        [volume] => 98
        [edition] => 2
        [name] => 9
        [type] => 2
    )

[test5] => Array
    (
        [volume] => 86
        [edition] => 6
        [name] => 10
        [type] => 2
    )

[test6] => Array
    (
        [volume] => 67
        [edition] => 7
        [name] => 11
        [type] => 2
    )

)

$sort = array(85, 67, 86, 98, 86, 67);
array_multisort($sort, $data);

这是原始数组:

$data['test1'] = array('volume' => 67, 'edition' => 2, 'name' => 6, 'num' => 2,);
$data['test2'] = array('volume' => 86, 'edition' => 1, 'name' => 7, 'type' => 2,);
$data['test3'] = array('volume' => 85, 'edition' => 6, 'name' => 8, 'type' => 2,);
$data['test4'] = array('volume' => 98, 'edition' => 2, 'name' => 9, 'type' => 2,);
$data['test5'] = array('volume' => 86, 'edition' => 6, 'name' => 10, 'type' => 2,);
$data['test6'] = array('volume' => 67, 'edition' => 7, 'name' => 11, 'type' => 2,);

1 个答案:

答案 0 :(得分:10)

使用数组Multisort,您可以根据列进行排序。这是一个示例,您可以通过“所有”可用列轻松排序:

<?
$data['test1'] = array('volume' => 67, 'edition' => 2, 'name' => 6, 'type' => 2);
$data['test2'] = array('volume' => 86, 'edition' => 1, 'name' => 7, 'type' => 2);
$data['test3'] = array('volume' => 85, 'edition' => 6, 'name' => 8, 'type' => 2);
$data['test4'] = array('volume' => 98, 'edition' => 2, 'name' => 9, 'type' => 2);
$data['test5'] = array('volume' => 86, 'edition' => 6, 'name' => 10, 'type' => 2);
$data['test6'] = array('volume' => 67, 'edition' => 7, 'name' => 11, 'type' => 2);

//Create index rows
foreach ($data as $row) {
  foreach ($row as $key => $value){
    ${$key}[]  = $value; //Creates $volume, $edition, $name and $type arrays.
  }  
}

//ex: sort by edition asc, then by name DESC:

array_multisort($edition, SORT_ASC, $name, SORT_DESC, $data);

echo "<pre>";
print_r($data);
echo "</pre>";
?>

将导致(首先是版本ASC,然后是名称DESC): (按名称ASC排序,将交换test4和test1 ofc。)

Array
(
    [test2] => Array
        (
            [volume] => 86
            [edition] => 1
            [name] => 7
            [type] => 2
        )

    [test4] => Array
        (
            [volume] => 98
            [edition] => 2
            [name] => 9
            [type] => 2
        )

    [test1] => Array
        (
            [volume] => 67
            [edition] => 2
            [name] => 6
            [type] => 2
        )

    [test5] => Array
        (
            [volume] => 86
            [edition] => 6
            [name] => 10
            [type] => 2
        )

    [test3] => Array
        (
            [volume] => 85
            [edition] => 6
            [name] => 8
            [type] => 2
        )

    [test6] => Array
        (
            [volume] => 67
            [edition] => 7
            [name] => 11
            [type] => 2
        )

)

如果您愿意,可以添加任意数量的列。我希望这会有所帮助。

编辑:关于你的问题: 不,Array MultiSort不用于根据预定义的顺序对数组进行排序。 array_multisort的作用是: 它按给定条件(asc或desc)对数组进行排序,并相对向上或向下移动其他数组中的条目,而不违反任何其他数组的排序

基本示例:

$letters = array("b","a","c");
$numbers = array(5,4,2);

调用array_multisort($letters,$numbers)会导致a,b,c并且(a已向上移动,b向下移动)4,5,2

如果示例是:

$letters = array("b","a","a");
$numbers = array(5,4,2);

首先应用相同的排序:(a,a,b - &gt; 4,2,5),然后是array_multisort通知,它可以将两个a交换到排序2和4。 REsult:a,a,b - &gt; 2,4,5

回到你的问题:

要按预定义顺序排序,您可以执行以下操作:

1。)定义您的订单,即$order = array("a","z","b");

2.。)使用用户功能调用uasort

3.。)在sort函数中,使用array flip和assoc访问来获取实际位置:

$items = array("a","b","z","a","z","z");

uasort($items, "sortByPredefinedOrder");

function sortByPredefinedOrder($leftItem, $rightItem){
  $order = array("a","z","b","x"); //defined somewhere

  $flipped = array_flip($order); //so we can access "position by value"

  $leftPos = $flipped[$leftItem];
  $rightPos = $flipped[$rightItem];
  return $leftPos >= $rightPos;   
}

print_r($items); //Array ( [0] => a [3] => a [2] => z [4] => z [5] => z [1] => b )

对于您的多维输入,您可以使用

  $leftPos = $flipped[$leftItem["volume"]];
  $rightPos = $flipped[$rightItem["volume"]];

但是。这将要求您预测预定义顺序数组中的所有值,并使用适当的返回值处理IndexOutOfBoundExceptions。