Nike
Nike

Reputation: 41

PHP - Combine sub-arrays and sort by value?

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:

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.

Upvotes: 2

Views: 4410

Answers (3)

Gordon
Gordon

Reputation: 316969

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

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.

Upvotes: 1

Matthijs Bierman
Matthijs Bierman

Reputation: 1757

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. Scan your input arrays for the lowest value, copy that value into your new array, delete the value from the input array.
  3. Repeat until all your input arrays are empty.

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

@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.

Upvotes: 0

Don Kirkby
Don Kirkby

Reputation: 56620

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

$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.

Upvotes: 3

Related Questions