0

Currently I have this json data

1st Data

{
    "id": 41,
    "userId": 9,
    "description": null,
    "updated_by": "Juan Dela Cruz",
    "created_at": "2019-09-18 10:07:34",
    "updated_at": "2019-09-18 10:07:34"
}

2nd Data

{
    "id": 2,
    "userId": 9,
    "description": null,
    "updated_by": null,
    "created_at": "2019-09-16 12:46:56",
    "updated_at": "2019-09-16 12:46:56"
}

I have this variable $myData if you try to echo this $myData the above json will be the expected output.

Is it possible to get the most recent created_at using this max() function of php?

My expected output would be

1st Data

{
    "id": 41,
    "userId": 9,
    "description": null,
    "updated_by": "Juan Dela Cruz",
    "created_at": "2019-09-18 10:07:34",
    "updated_at": "2019-09-18 10:07:34"
}

Since the created_at is recently created than to the 2nd data.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Pablo
  • 1,357
  • 1
  • 11
  • 40
  • if is it possible you can just do order by from db if you are fetching it from your own local db – Jaymin Sep 18 '19 at 05:57
  • Unfortunately can't do that in my situation.. Of course in my db I can simply query something like this `ORDER BY created_at DESC LIMIT 1` but in my php query I can't simply do this. – Pablo Sep 18 '19 at 05:57
  • If you know how to decode the data - https://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date gives all you need. – Nigel Ren Sep 18 '19 at 06:27

4 Answers4

0

Try

SELECT * FROM tbl WHERE 1 = 1 ORDER BY created_at DESC LIMIT 1,1; OR SELECT * FROM tbl ORDER BY created_at DESC LIMIT 1,1;

Mudasir Syed
  • 121
  • 7
0

Make one array of two json data and keep key as after converting created_at using strtotime(). Then use max() in all array_keys() and print the array.

$a = '{"id":41,"userId":9,"description":null,"updated_by":"Juan Dela Cruz","created_at":"2019-09-18 10:07:34","updated_at":"2019-09-18 10:07:34"}';
$b = '{"id":2,"userId":9,"description":null,"updated_by":null,"created_at":"2019-09-16 12:46:56","updated_at":"2019-09-16 12:46:56"}';
$arr1 = json_decode($a, true);
$key1 = strtotime($arr1['created_at']);
$arr[$key1] = $arr1;
$arr2 = json_decode($b, true);
$key2 = strtotime($arr2['created_at']);
$arr[$key2] = $arr2;
//print_r($arr);
$key = max(array_keys($arr));
print_r($arr[$key]);

Demo

Bhaskar Jain
  • 1,651
  • 1
  • 12
  • 20
0

This is how you can achieve it using usort function.

<?php
// $json = "{"id":41,"userId":9,"description":null,"updated_by":"Juan Dela Cruz","created_at":"2019-09-18 10:07:34","updated_at":"2019-09-18 10:07:34"}";
$arr[] = array('id' => 41, "userId" => 9,"created_at" => "2019-09-18 10:07:34");
$arr[] = array('id' => 42, "userId" => 10,"created_at" => "2019-09-16 12:46:56");
echo '<pre>';
print_r($arr);
usort($arr, function($a, $b) {
  $ad = new DateTime($a['created_at']);
  $bd = new DateTime($b['created_at']);

  if ($ad == $bd) {
    return 0;
  }

  return $ad < $bd ? -1 : 1;
});
echo '<pre>';
print_r($arr);

?>

I am supposing that my $arr is it in this format.

Array
(
    [0] => Array
        (
            [id] => 41
            [userId] => 9
            [created_at] => 2019-09-18 10:07:34
        )

    [1] => Array
        (
            [id] => 42
            [userId] => 10
            [created_at] => 2019-09-16 12:46:56
        )

)

After Sorting, your array will be like this:

Array
(

        [0] => Array
            (
                [id] => 42
                [userId] => 10
                [created_at] => 2019-09-16 12:46:56
            )

        [1] => Array
            (
                [id] => 41
                [userId] => 9
                [created_at] => 2019-09-18 10:07:34
            )

    )
Jaymin
  • 1,643
  • 1
  • 18
  • 39
0
<?php
$json = '{
"0":{
    "id": 41,
    "userId": 9,
    "description": null,
    "updated_by": "Juan Dela Cruz",
    "created_at": "2019-09-18 10:07:34",
    "updated_at": "2019-09-18 10:07:34"
},
"1":{
    "id": 2,
    "userId": 9,
    "description": null,
    "updated_by": null,
    "created_at": "2019-09-16 12:46:56",
    "updated_at": "2019-09-16 12:46:56"
}

}';
$jsonDecoded = json_decode($json, true);

function array_sort_by_column(&$arr, $col, $dir = SORT_DESC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}


array_sort_by_column($jsonDecoded, 'created_at');

print_r($jsonDecoded);
?>

Output Will be:

Array
(
    [0] => Array
        (
            [id] => 41
            [userId] => 9
            [description] => 
            [updated_by] => Juan Dela Cruz
            [created_at] => 2019-09-18 10:07:34
            [updated_at] => 2019-09-18 10:07:34
        )

    [1] => Array
        (
            [id] => 2
            [userId] => 9
            [description] => 
            [updated_by] => 
            [created_at] => 2019-09-16 12:46:56
            [updated_at] => 2019-09-16 12:46:56
        )

)

If you want to change the sorting order then change the parameter $dir to $dir = SORT_ASC, that is : function array_sort_by_column(&$arr, $col, $dir = SORT_ASC)

Then, To get the recent created_at from the array you can add the following code (If the sort order is SORT_DESC then use min() else use max() ):

print_r($jsonDecoded[min(array_keys($jsonDecoded))]);
MjM
  • 571
  • 4
  • 15