简体   繁体   English

如何使用usort对MongoDB查询结果进行排序

[英]How to use usort to sort MongoDB query result

Trying to sort array present in object but getting error usort() expects parameter 1 to be array I have not found any useful solution for this error on anywhere, How can I sort my array[detail] by total array elements count.尝试对对象中存在的数组进行排序但出现错误usort() 期望参数 1 为数组我在任何地方都没有找到针对此错误的任何有用解决方案,如何按数组元素总数对数组 [详细信息] 进行排序。

Here is my code -这是我的代码 -

$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery("DB.col", $query);
foreach($cursor as $row) {
  foreach($row->detail as $item) {
    function cmp($a, $b) {
      return $a['detail'] > $b['detail'];
    }
    usort($row, "cmp");
  }
} 

Schema of my array:我的阵列的架构:

array(
    [0]=>stdclass object( 
        [_id] => MongoDB\BSON\ObjectId Object (
                    [oid] => 5f55f95815a8508e2deac8dd
                )
        [Date] => 9/12/2020
        [title] => ram
        [roll.n] => 5
        [detail] => Array(
            [0] => stdclass object( 
                [title] => John
                [id] =>55
                [class] => six)
            [1] => stdclass object( 
                [title] => Doe
                [id] =>550
                [class] => six)
            [2] => stdclass object( 
                [title] => Jean
                [id] =>9
                [class] => one)
            [3] => stdclass object( 
                [title] => AI
                [id] =>90
                [class] => one)
            )
    )

    [1]=>stdclass object( 
        [_id] => MongoDB\BSON\ObjectId Object (
                [oid] => 5f
        )
        [title] => sunny
        [roll.n] => 50
        [detail] => Array(
            [0] => stdclass object ( 
                [title] => lilly
                [id] =>551
                [class] => six)
            [1] => stdclass object( 
                [title] => Doel
                [id] =>550
                [class] => six)
            [2] => stdclass object( 
                [title] => rehaman
                [id] =>9
                [class] => one
        )
    )
)

The executeQuery method returns a MongoDB\\Driver\\Cursor . executeQuery方法返回一个MongoDB\\Driver\\Cursor It implements the Traversable interface so you are allowed to do foreach on it.它实现了Traversable接口,因此您可以对其进行foreach操作。 This might have confused you to think it is an array.这可能会让您误以为它是一个数组。 It's actually not.其实不是。

To do usort on the result, you need to first turn the cursor into an array.要对结果进行排序,您需要先将游标转换为数组。 Then, if I understand correctly, you are to sort the rows by the size of their 'detail' array like this:然后,如果我理解正确,您将按“详细信息”数组的大小对行进行排序,如下所示:

$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery("DB.col", $query);
$rows = $cursor->toArray();

usort($rows, function ($a, $b) {
  return sizeof($a->detail) <=> sizeof($b->detail);
});

return $rows;

Note: if the order is reversed, simply swap the $a->detail and $b->detail in the comparison function of usort() call.注意:如果顺序颠倒了,只需在usort()调用的比较函数中交换$a->detail$b->detail

If you're not using PHP 7, you might not have the spaceship operator <=> to use with.如果您没有使用 PHP 7,您可能没有飞船操作符<=>可以使用。 Simply use this comparison function instead:只需使用此比较函数:

usort($rows, function ($a, $b) {
  if (sizeof($a->detail) == sizeof($b->detail)) {
    return 0;
  }
  return (sizeof($a->detail) > sizeof($b->detail)) ? 1 : -1;
});

PS You should really upgrade to PHP 7. PHP 5.6 has been dropped from support for years. PS 您真的应该升级到 PHP 7。多年来,PHP 5.6 已不再受支持。 It's not as performant and less secure.它的性能和安全性都没有那么好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM