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!

Kevin
  • 41,694
  • 12
  • 53
  • 70
greW
  • 1,248
  • 3
  • 24
  • 49
  • Did you try usort function ? It will do the work. – Somnath Paul Sep 14 '14 at 13:32
  • possible duplicate of [Reference: all basic ways to sort arrays and data in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) – Kevin Sep 14 '14 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]);`` ? – greW Sep 14 '14 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. – greW Sep 14 '14 at 13:38

1 Answers1

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

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • ``Call to a member function getTimestamp() on a non-object ``, in line ``return $v1->getTimestamp() - $v2->getTimestamp();`` – greW Sep 14 '14 at 13:53
  • @greW check my revision, its weird why `->getTimestamp()` is not working – Kevin Sep 14 '14 at 13:56
  • 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 Sep 14 '14 at 13:58
  • 1
    @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 – Kevin Sep 14 '14 at 14:04
  • @greW have you checked my demo friend? actually i just based your array sample on mine, maybe they're not similar on your environment – Kevin Sep 14 '14 at 14:11