php multidimensional sorting

Sorting arrays with more than one dimension, or by something other than alphabetical or numerical order, is more complicated. PHP knows how to compare two numbers or two text strings, but in a multidimensional array, each element is an array.

There are two approaches to sorting multidimensional arrays: creating a user-defined sort or using the array_multisort() function

Using array_multisort() to sort array

The array_multisort() function can be used either to sort multidimensional arrays, or to sort multiple arrays at once

The following is the definition of a two-dimensional array used earlier. This array stores Bob’s three products with a code, a description, and a price for each

$products = array(array('TIR', 'Tires', 100),
array('OIL', 'Oil', 10),
array('SPK', 'Spark Plugs', 4));

array_multisort($products);

Leave a Comment