I see that there is a lot of documentation for ordering array, but all I find is for ordering existing values within an array.
In my case, I need to sort the array themselves, within a meta that includes multiple array. And I want to order them in the reverse order of their creation.
Example:
$datacomments = get_post_meta($product_id, 'propina5', false);
echo print_r($datacomments);
//Result:
Array ( [0] => Array ( [date] => 30-11-2021 13:38 [id] => 2 [rating] => 4 [comment] => bala bla bla bla bla [perce] => 10 )
[1] => Array ( [date] => 30-11-2021 13:38 [id] => 2 [rating] => 4 [comment] => bala bla bla bla bla [perce] => 10 )
[2] => Array ( [date] => 30-11-2021 13:40 [id] => 2 [rating] => 5 [comment] => bla bla bla bla [perce] => 10 )
[3] => Array ( [date] => 30-11-2021 13:41 [id] => 2 [rating] => 3 [comment] => bla bla bla bla bla [perce] => 5 ) ) 1
I have 3 arrays the [0], [1], [2] and the [3] ordered by default as they were created. But I need to exactly order them in reverse order since my intention is that the most recent ones appear first and not the other way around.
Well I can’t find documentation or can’t see if it’s there in either official php or w3school, where I usually look for how to sort in this case.
Any suggestion? From already thank you very much.
>Solution :
$reversed = array_reverse($input);
Docs: https://www.php.net/manual/en/function.array-reverse.php
If this array was the result of an query from database, you can use ORDER BY syntax from sql to get exactly you need.