2

I have this Array,

Array
(
    [0] => Array
        (
            [ID] => 108
            [custom] => Array
                (
                    [date_tps] => Array
                        (
                            [0] => 14.07.2014
                        )
                )
        )

    [1] => Array
        (
            [ID] => 123
            [custom] => Array
                (
                    [date_tps] => Array
                        (
                            [0] => 22.07.2014
                        )
                )
        )

    [2] => Array
        (
            [ID] => 152
            [custom] => Array
                (
                    [date_tps] => Array
                        (
                            [0] => 23.06.2014
                        )
                )
        )

    [3] => Array
        (
            [ID] => 153
            [custom] => Array
                (
                    [date_tps] => Array
                        (
                            [0] => 06.07.2014
                        )
                )
        )
)

and I'm trying to sort it by date, date_tps[0] field, from the new date to the old one. can someone guide me how to make it done ? I've tried using the php function to sort (those with $a-$b) but it didnt work (I guess its because its really inside many arrays. thanks a lot!

4
  • Did you try usort function ? It will do the work. Commented Sep 14, 2014 at 13:32
  • possible duplicate of Reference: all basic ways to sort arrays and data in PHP Commented Sep 14, 2014 at 13:34
  • I tried this, but It didn't work out, I wasn't sure how to pass the values, like `` $a = strtotime($a[custom][date_tps][0]); $b = strtotime($b[custom][date_tps][0]);`` ? Commented Sep 14, 2014 at 13:35
  • @Ghost, why whould I build the whole function of sorting if php give me a few solution for sorting as built in functions ? the problem is that I can make the sort work on multiple array in array, and thats the main question here. Commented Sep 14, 2014 at 13:38

1 Answer 1

1

Yes you need usort() to this:

The simple hurdle is that you need to convert the times inside first before they are comparable.

$values = array(
    array('ID' => 108, 'custom' => array('date_tps' => array('14.07.2014'))),
    array('ID' => 123, 'custom' => array('date_tps' => array('22.07.2014'))),
    array('ID' => 152, 'custom' => array('date_tps' => array('23.06.2014'))),
    array('ID' => 108, 'custom' => array('date_tps' => array('06.07.2014'))),
);

usort($values, function($a, $b){
    $v1 = $a['custom']['date_tps'][0];
    $v2 = $b['custom']['date_tps'][0];
    $v1 = DateTime::createFromFormat('d.m.Y', $v1); // convert them properly first
    $v2 = DateTime::createFromFormat('d.m.Y', $v2);
    return $v1->getTimestamp() - $v2->getTimestamp();
    // or
    return $v1->format('U') - $v2->format('U');
});

echo '<pre>';
print_r($values);

Demo

Sign up to request clarification or add additional context in comments.

5 Comments

Call to a member function getTimestamp() on a non-object , in line return $v1->getTimestamp() - $v2->getTimestamp();
@greW check my revision, its weird why ->getTimestamp() is not working
first option returns Call to a member function getTimestamp() on a non-object and second returns Call to a member function format() on a non-object
@greW are you sure that the values thats inside your question and the one i made as dummy data are similar, its just based on your example anyway
@greW have you checked my demo friend? actually i just based your array sample on mine, maybe they're not similar on your environment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.