PHP - 组合子数组并按值排序?

时间:2021-11-29 12:22:02

This is what i've got now:

这就是我现在所拥有的:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 53
                    [date] => 18 Sep 2010 10:29
                    [user] => 52
                    [post] => ytiuy
                )

            [1] => Array
                (
                    [id] => 55
                    [date] => 11 Sep 2010 11:14
                    [user] => 52
                    [post] => this is a test post :]
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 56
                    [date] => 4 Sep 2010 03:19
                    [user] => 55
                    [post] => pppost :DD:D:D:D
                )

        )

)

I want to remove the first two "steps" in the array, and then sort the array by the 'date' value, like this:

我想删除数组中的前两个“步骤”,然后按'date'值对数组进行排序,如下所示:

Array
(
    [0] => Array
        (
            [id] => 56
            [date] => 4 Sep 2010 03:19
            [user] => 55
            [post] => pppost :DD:D:D:D
        )

    [1] => Array
        (
            [id] => 55
            [date] => 11 Sep 2010 11:14
            [user] => 52
            [post] => this is a test post :]
        )

    [2] => Array
        (
            [id] => 53
            [date] => 18 Sep 2010 10:29
            [user] => 52
            [post] => ytiuy
        )
)

Any ideas?

有任何想法吗?

Thanks a bunch, appreciate all help! :)

非常感谢,感谢所有帮助! :)

EDIT: I should also mention that the amount of arrayitems will not always be the same.

编辑:我还应该提到,arrayitems的数量并不总是相同。

3 个解决方案

#1


3  

You should be able to use an accumulator pattern with the array_merge function to merge all the lower level arrays together.

您应该能够使用带有array_merge函数的累加器模式将所有较低级别的数组合并在一起。

$result = array();
foreach ($oldarray as $child)
{
    $result = array_merge($result, $child);
}

Finally, you can use the user defined sort function to sort the whole thing.

最后,您可以使用用户定义的排序函数对整个事物进行排序。

#2


1  

An alternative to Don Kirby's solution would be to use an SplMaxHeap which would allow you to iterate and sort in one go:

Don Kirby的解决方案的替代方案是使用SplMaxHeap,它允许您一次迭代和排序:

class PostHeap extends SplMaxHeap
{
    public function compare($post, $other)
    {
        return strtotime($post['date']) - strtotime($other['date']);
    }
}

$postHeap = new PostHeap;
foreach($posts as $subArray) {
    foreach($subArray as $post) {
        $postHeap->insert($post);
    }
}

The $postHeap would then contain the posts in descending date order, e.g. newest date first. You can use the code in the compare function if you want to use usort instead. The order will be ascending then though.

然后,$ postHeap将按降序日期顺序包含帖子,例如最新的约会。如果要使用usort,可以使用compare函数中的代码。然而,订单将会升序。

#3


0  

Do you have two arrays? Or more? Are they already sorted? If so, you can use that to combine them more efficiently. If not, you probably need to sort them first.

你有两个阵列吗?或者更多?它们已经排序了吗?如果是这样,您可以使用它来更有效地组合它们。如果没有,您可能需要先对它们进行排序。

Roughly:

大致:

  1. Sort your input arrays (optionally)
  2. 对输入数组进行排序(可选)
  3. Scan your input arrays for the lowest value, copy that value into your new array, delete the value from the input array.
  4. 扫描输入数组以获取最低值,将该值复制到新数组中,从输入数组中删除该值。
  5. Repeat until all your input arrays are empty.
  6. 重复,直到所有输入数组都为空。

Of course, if you don't care about performance at all you could simply combine all the arrays and then sort that.

当然,如果你根本不关心性能,你可以简单地组合所有数组然后对它进行排序。

And for sorting you may want to use: http://www.php.net/manual/en/function.sort.php#99700

对于排序,您可能需要使用:http://www.php.net/manual/en/function.sort.php#99700

@Don Kirkby: Indeed: It's basically a mergesort, but it only works on already sorted arrays. If they're both unsorted you're probably better off with combining them and using quicksort instead.

@Don Kirkby:确实:它基本上是一个mergesort,但它只适用于已排序的数组。如果它们都没有被排除,那么你最好将它们组合起来并使用quicksort。

#1


3  

You should be able to use an accumulator pattern with the array_merge function to merge all the lower level arrays together.

您应该能够使用带有array_merge函数的累加器模式将所有较低级别的数组合并在一起。

$result = array();
foreach ($oldarray as $child)
{
    $result = array_merge($result, $child);
}

Finally, you can use the user defined sort function to sort the whole thing.

最后,您可以使用用户定义的排序函数对整个事物进行排序。

#2


1  

An alternative to Don Kirby's solution would be to use an SplMaxHeap which would allow you to iterate and sort in one go:

Don Kirby的解决方案的替代方案是使用SplMaxHeap,它允许您一次迭代和排序:

class PostHeap extends SplMaxHeap
{
    public function compare($post, $other)
    {
        return strtotime($post['date']) - strtotime($other['date']);
    }
}

$postHeap = new PostHeap;
foreach($posts as $subArray) {
    foreach($subArray as $post) {
        $postHeap->insert($post);
    }
}

The $postHeap would then contain the posts in descending date order, e.g. newest date first. You can use the code in the compare function if you want to use usort instead. The order will be ascending then though.

然后,$ postHeap将按降序日期顺序包含帖子,例如最新的约会。如果要使用usort,可以使用compare函数中的代码。然而,订单将会升序。

#3


0  

Do you have two arrays? Or more? Are they already sorted? If so, you can use that to combine them more efficiently. If not, you probably need to sort them first.

你有两个阵列吗?或者更多?它们已经排序了吗?如果是这样,您可以使用它来更有效地组合它们。如果没有,您可能需要先对它们进行排序。

Roughly:

大致:

  1. Sort your input arrays (optionally)
  2. 对输入数组进行排序(可选)
  3. Scan your input arrays for the lowest value, copy that value into your new array, delete the value from the input array.
  4. 扫描输入数组以获取最低值,将该值复制到新数组中,从输入数组中删除该值。
  5. Repeat until all your input arrays are empty.
  6. 重复,直到所有输入数组都为空。

Of course, if you don't care about performance at all you could simply combine all the arrays and then sort that.

当然,如果你根本不关心性能,你可以简单地组合所有数组然后对它进行排序。

And for sorting you may want to use: http://www.php.net/manual/en/function.sort.php#99700

对于排序,您可能需要使用:http://www.php.net/manual/en/function.sort.php#99700

@Don Kirkby: Indeed: It's basically a mergesort, but it only works on already sorted arrays. If they're both unsorted you're probably better off with combining them and using quicksort instead.

@Don Kirkby:确实:它基本上是一个mergesort,但它只适用于已排序的数组。如果它们都没有被排除,那么你最好将它们组合起来并使用quicksort。