Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php数组\u多重排序列我没有要求排序_Php_Sorting - Fatal编程技术网

php数组\u多重排序列我没有要求排序

php数组\u多重排序列我没有要求排序,php,sorting,Php,Sorting,我正在使用array\u multisort按一列对多维数组进行排序。是的,我知道这个函数不需要只按一列排序,但这是用于可能有更多列被排序的代码,而现在只有一个测试失败 当我运行在interwebs上发现的测试时,它运行良好。当我对其他一些测试数据运行几乎完全相同的东西时,它也会对一个我没有要求它排序的列进行排序 此测试返回正确排序的数据(值未排序): 转储数据: array(7) { [0]=> array(4) { ["id"]=> int(2) ["name"]=> st

我正在使用array\u multisort按一列对多维数组进行排序。是的,我知道这个函数不需要只按一列排序,但这是用于可能有更多列被排序的代码,而现在只有一个测试失败

当我运行在interwebs上发现的测试时,它运行良好。当我对其他一些测试数据运行几乎完全相同的东西时,它也会对一个我没有要求它排序的列进行排序

此测试返回正确排序的数据(值未排序):

转储数据:

array(7) { [0]=> array(4) { ["id"]=> int(2) ["name"]=> string(3) "abc" ["value"]=> string(3) "xyz" ["order"]=> int(2) } [1]=> array(4) { ["id"]=> int(4) ["name"]=> string(3) "def" ["value"]=> string(3) "xyz" ["order"]=> int(2) } [2]=> array(4) { ["id"]=> int(5) ["name"]=> string(3) "ghi" ["value"]=> string(3) "uvw" ["order"]=> int(3) } [3]=> array(4) { ["id"]=> int(6) ["name"]=> string(3) "ghi" ["value"]=> string(3) "def" ["order"]=> int(3) } [4]=> array(4) { ["id"]=> int(7) ["name"]=> string(3) "ghi" ["value"]=> string(3) "fff" ["order"]=> int(3) } [5]=> array(4) { ["id"]=> int(3) ["name"]=> string(3) "uvw" ["value"]=> string(3) "ghi" ["order"]=> int(1) } [6]=> array(4) { ["id"]=> int(1) ["name"]=> string(3) "xyz" ["value"]=> string(3) "abc" ["order"]=> int(6) } }
array(10) { [0]=> array(3) { ["name"]=> string(3) "bag" ["quantity"]=> int(150) ["color"]=> string(4) "blue" } [1]=> array(3) { ["name"]=> string(3) "bag" ["quantity"]=> int(500) ["color"]=> string(5) "green" } [2]=> array(3) { ["name"]=> string(3) "box" ["quantity"]=> int(1000) ["color"]=> string(3) "red" } [3]=> array(3) { ["name"]=> string(3) "box" ["quantity"]=> int(1200) ["color"]=> string(4) "pink" } [4]=> array(3) { ["name"]=> string(8) "notebook" ["quantity"]=> int(50) ["color"]=> string(4) "blue" } [5]=> array(3) { ["name"]=> string(8) "notebook" ["quantity"]=> int(100) ["color"]=> string(6) "yellow" } [6]=> array(3) { ["name"]=> string(8) "notebook" ["quantity"]=> int(250) ["color"]=> string(6) "orange" } [7]=> array(3) { ["name"]=> string(7) "package" ["quantity"]=> int(1000) ["color"]=> string(5) "brown" } [8]=> array(3) { ["name"]=> string(7) "package" ["quantity"]=> int(1500) ["color"]=> string(5) "green" } [9]=> array(3) { ["name"]=> string(6) "pencil" ["quantity"]=> int(100) ["color"]=> string(6) "yellow" } }
此测试在“名称”字段上正确排序,但也在我未请求的“数量”字段上排序,并且不希望对该列进行排序:

$objectListData4 =  [
        ["name" => "package", "quantity" => 1000, "color" => "brown"],
        ["name" => "box", "quantity" => 1000, "color" => "red"],
        ["name" => "notebook", "quantity" => 250, "color" => "orange"],
        ["name" => "pencil", "quantity" => 100, "color" => "yellow"],
        ["name" => "bag", "quantity" => 150, "color" => "blue"],
        ["name" => "box", "quantity" => 1200, "color" => "pink"],
        ["name" => "notebook", "quantity" => 50, "color" => "blue"],
        ["name" => "package", "quantity" => 1500, "color" => "green"],
        ["name" => "bag", "quantity" => 500, "color" => "green"],
        ["name" => "notebook", "quantity" => 100, "color" => "yellow"],
    ];

$tmp = array_column($objectListData4, 'name');
array_multisort($tmp, SORT_ASC, $objectListData4);

var_dump($objectListData4); // note the name 'notebook' and the order of the 'quantity' column.  the 'quantity' column has also been sorted but shouldnt be
转储数据:

array(7) { [0]=> array(4) { ["id"]=> int(2) ["name"]=> string(3) "abc" ["value"]=> string(3) "xyz" ["order"]=> int(2) } [1]=> array(4) { ["id"]=> int(4) ["name"]=> string(3) "def" ["value"]=> string(3) "xyz" ["order"]=> int(2) } [2]=> array(4) { ["id"]=> int(5) ["name"]=> string(3) "ghi" ["value"]=> string(3) "uvw" ["order"]=> int(3) } [3]=> array(4) { ["id"]=> int(6) ["name"]=> string(3) "ghi" ["value"]=> string(3) "def" ["order"]=> int(3) } [4]=> array(4) { ["id"]=> int(7) ["name"]=> string(3) "ghi" ["value"]=> string(3) "fff" ["order"]=> int(3) } [5]=> array(4) { ["id"]=> int(3) ["name"]=> string(3) "uvw" ["value"]=> string(3) "ghi" ["order"]=> int(1) } [6]=> array(4) { ["id"]=> int(1) ["name"]=> string(3) "xyz" ["value"]=> string(3) "abc" ["order"]=> int(6) } }
array(10) { [0]=> array(3) { ["name"]=> string(3) "bag" ["quantity"]=> int(150) ["color"]=> string(4) "blue" } [1]=> array(3) { ["name"]=> string(3) "bag" ["quantity"]=> int(500) ["color"]=> string(5) "green" } [2]=> array(3) { ["name"]=> string(3) "box" ["quantity"]=> int(1000) ["color"]=> string(3) "red" } [3]=> array(3) { ["name"]=> string(3) "box" ["quantity"]=> int(1200) ["color"]=> string(4) "pink" } [4]=> array(3) { ["name"]=> string(8) "notebook" ["quantity"]=> int(50) ["color"]=> string(4) "blue" } [5]=> array(3) { ["name"]=> string(8) "notebook" ["quantity"]=> int(100) ["color"]=> string(6) "yellow" } [6]=> array(3) { ["name"]=> string(8) "notebook" ["quantity"]=> int(250) ["color"]=> string(6) "orange" } [7]=> array(3) { ["name"]=> string(7) "package" ["quantity"]=> int(1000) ["color"]=> string(5) "brown" } [8]=> array(3) { ["name"]=> string(7) "package" ["quantity"]=> int(1500) ["color"]=> string(5) "green" } [9]=> array(3) { ["name"]=> string(6) "pencil" ["quantity"]=> int(100) ["color"]=> string(6) "yellow" } }

有人知道为什么第二个测试在不应该的情况下也对数量进行排序吗?

我不确定错误的原因是什么,我怀疑是未记录的功能导致array_multisort通过查找下一个可用的关联键或其他东西对数组进行排序

但确保不再进行进一步排序的解决方案是将数组键作为第二个排序参数传入。第二个给定示例的工作版本:

$objectListData4 =  [
        ["name" => "package", "quantity" => 1000, "color" => "brown"],
        ["name" => "box", "quantity" => 1000, "color" => "red"],
        ["name" => "notebook", "quantity" => 250, "color" => "orange"],
        ["name" => "pencil", "quantity" => 100, "color" => "yellow"],
        ["name" => "bag", "quantity" => 150, "color" => "blue"],
        ["name" => "box", "quantity" => 1200, "color" => "pink"],
        ["name" => "notebook", "quantity" => 50, "color" => "blue"],
        ["name" => "package", "quantity" => 1500, "color" => "green"],
        ["name" => "bag", "quantity" => 500, "color" => "green"],
        ["name" => "notebook", "quantity" => 100, "color" => "yellow"],
    ];

$tmp = array_column($objectListData4, 'name');
array_multisort($tmp, SORT_ASC, array_keys($objectListData4), $objectListData4);

var_dump($objectListData4); // note the name 'notebook' and the order of the 'quantity' column.  the 'quantity' column has also been sorted but shouldnt be
它的输出:

array(10) {
  [0]=>
  array(3) {
    ["name"]=>
    string(3) "bag"
    ["quantity"]=>
    int(150)
    ["color"]=>
    string(4) "blue"
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(3) "bag"
    ["quantity"]=>
    int(500)
    ["color"]=>
    string(5) "green"
  }
  [2]=>
  array(3) {
    ["name"]=>
    string(3) "box"
    ["quantity"]=>
    int(1000)
    ["color"]=>
    string(3) "red"
  }
  [3]=>
  array(3) {
    ["name"]=>
    string(3) "box"
    ["quantity"]=>
    int(1200)
    ["color"]=>
    string(4) "pink"
  }
  [4]=>
  array(3) {
    ["name"]=>
    string(8) "notebook"
    ["quantity"]=>
    int(250)
    ["color"]=>
    string(6) "orange"
  }
  [5]=>
  array(3) {
    ["name"]=>
    string(8) "notebook"
    ["quantity"]=>
    int(50)
    ["color"]=>
    string(4) "blue"
  }
  [6]=>
  array(3) {
    ["name"]=>
    string(8) "notebook"
    ["quantity"]=>
    int(100)
    ["color"]=>
    string(6) "yellow"
  }
  [7]=>
  array(3) {
    ["name"]=>
    string(7) "package"
    ["quantity"]=>
    int(1000)
    ["color"]=>
    string(5) "brown"
  }
  [8]=>
  array(3) {
    ["name"]=>
    string(7) "package"
    ["quantity"]=>
    int(1500)
    ["color"]=>
    string(5) "green"
  }
  [9]=>
  array(3) {
    ["name"]=>
    string(6) "pencil"
    ["quantity"]=>
    int(100)
    ["color"]=>
    string(6) "yellow"
  }
}
也许这个问题可以帮助你,另一方面,也许usort()是一个选择

usort($objectListData4, 'cmp');

function cmp($a, $b) {
    return strcmp($a['name'], $b['name']);
}

简短回答:您不能使用array_multisort()实现您想要的结果,因为如果两个成员进行相等的比较,则它们在排序数组中的相对顺序是未定义的。(见中的注释)

这意味着你不能指望你的“笔记本”保持原来的秩序。如果他们碰巧这么做,那只是巧合

这里有一个解决方案

function jnlsort($a, $b)//if the names are same, sort by index. Otherwise, sort by name.
{
    if (($a['name'] == $b['name']))
    {
        return strcmp($a['index'], $b['index']);
    }

    return strcmp($a['name'], $b['name']);
}

foreach($objectListData4 as $index => $obj)//add the index fields to preserve the order
{
    $objectListData4[$index]["index"] = $index;
}

usort($objectListData4, 'jnlsort');

foreach($objectListData4 as $index => $obj)//remove the index fields
{
    unset($objectListData4[$index]["index"]);
}

这是记录在案的行为。在手册中,对于第一个示例,它指出“第二个数组中的条目与第一个数组中的相同条目(100和100)也进行了排序”。在您的情况下,这将是第二个数组
$objectListData4
中的条目,对应于第一个数组
$tmp
中相同的
“notebook”
值。第一个数组中没有“100”。第一个数组只包含键和名称,这就是它-没有数量。引用引用的是示例数组,其中第一个数组的值为“100”。在您的例子中,这相当于第一个数组具有多个“notebook”值,而第二个数组则根据数量进行排序@GeorgeAppleton的答案是正确的,您应该接受它。在这种特定情况下,这确实解决了它。虽然不知道是什么原因导致它对一个数据集(而不是另一个数据集)执行未请求的二次排序,但这让我怀疑这个修复是否在所有情况下都能修复它,或者它看起来仍然是随机的。不知道是什么原因导致了最初的问题,这让我想知道,这是否只是一个问题,有时只有一列被排序,或者当有多个列时,它是否会发生。需要深入php的内部才能找到答案。无论如何,它都会修复它,我添加的代码有效地禁用了进一步的二次排序。只需将排序数组放在前面,将要排序的数组放在后面