Array reverse not working for Datetime value in PHP
Return array value is like this.
$ArrayData = Array ( [0] => Array ( [id] => 3 [username] =>Demo 1 [created_at] => 2022-01-05 17:14:46 ) [1] => Array ( [id] => 4 [username] => Demo 2 [created_at] => 2022-01-03 17:14:46 ) [2] => Array ( [id] => 5 [username] => Demo 3 [created_at] => 2022-01-07 17:14:46 ) [3] => Array ( [id] => 6 [username] => Demo 3 [created_at] => ) )
I am using array reverse, but it not working properly, I want to reverse the array by DateTime, but it reverses by username. How can I sort an array based on key-value DateTime in PHP?
I have got the solution to apply this code. Apply it.
<?php $SetOrd = array(); foreach ($ArrayData as $key => $value){ if(!empty($value['created_at'])) { $SetOrd[] = strtotime($value['created_at']); }else { $SetOrd[] = 0; } } array_multisort($SetOrd, SORT_DESC, $ArrayData); $MyArray = $ArrayData; echo "<pre>"; print_r($MyArray); ?>
Hope it will be helpful for you.
Question and answer is powered by AnsPress.io