I have an array that is given to me in random order. The array is an array of arrays, and I am given an order to place the array in based on a key that exists in each sub-array.
For example, I am given the array array(array('id' => 1), array('id' => 2)). I am told to order the arrays in the order (2,1) based upon the key ‘id’ of each sub-array.
What is the most efficient way to do this in PHP?
PHP does allow you to write your own sort functions. I can’t vouch for the efficiency of this solution, but it will work and is supported by the language directly, so I like it.
For your case you would write something like this:
You would then call this function as follows:
So, if for example you have the following starting array:
You’ll receive the following result: