Skip to content
Advertisement

Add order column to array to indicate rank from oldest to youngest

In PHP7, If I have this array:

JavaScript

What is the most elegant way to process this array to add an extra column indicating who is the oldest to who is the youngest like so…

JavaScript

Advertisement

Answer

Without a point of reference to determine which is the oldest user you’re stuck either doing a multi loop comparison or a sort then insert.

A Multi loop comparasin would look something like

JavaScript

Sort then insert would involve array_walk and uasort

From the docs

JavaScript

and docs

JavaScript

so for your example it would be something like this:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement