views:

61

answers:

3

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.

A: 

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.

Matthijs Bierman
If the initial arrays are unsorted, is sorting before combining any faster? I wouldn't think so, and the code is certainly simpler to combine, then sort.
Don Kirkby
+2  A: 

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.

Don Kirkby
The array_merge function was exactly what i was looking for. I tried it before i posted here, but i didn't get it to work. I forgot to make $result an array before the loop. The sort appears to be trickier though. Is it really possible to sort if it contains characters and numbers? Thanks for the post! :)
Nike
@Nike, do you mean that your dates are in string format, not date objects? If so, you can use the `strptime` function to parse them. You could either do that in the user defined comparison function that you pass to `usort`, or replace all the strings with date objects before sorting.
Don Kirkby
Sorry for the late respond. Yes, the date was in a string format, but i changed it to a unix timestamp instead and it's working very well now. It's much easier to sort the array by numbers, and then print out the date in human date format for thee user to read. Thanks a bunch, it would've taken weeks without your help. :)
Nike
Glad to help, @Nike, and welcome to Stack Overflow. Since you're new, you might not have noticed the big check mark next to each answer on your questions. You can click that to indicate that you've accepted an answer as the most helpful. It also gives a few points to the person who answered.
Don Kirkby
+1  A: 

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.

Gordon
I'm not sure, but don't you need another nested for loop to make that work?
Don Kirkby
@DonK thanks, basically my answer was more or less the same after correction, so I removed the duplicate parts.
Gordon