Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to order the arrays in the reverse order of their creation with php?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


$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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading