Sorting by a specific key of a multidimensional PHP array

I have an array of arrays. The internal array is as follows.

 Array
    (
        [comparisonFeatureId] => 1188
        [comparisonFeatureType] => Category
        [comparisonValues] => Array
            (
                [0] => Not Available
                [1] => Not Available
                [2] => Not Available
                [3] => Standard
            )

        [featureDescription] => Rear Reading Lamps
        [groupHeader] => Convenience
    )

So, I have an array of the above array, and I need to sort the array by FeatureDescription. Is there a way to do this using one of the PHP internal functions?

+2
source share
2 answers

See a list of all PHP sorting functions here: http://php.net/manual/en/array.sorting.php

You probably want to usort().

<?php

function myCmp($a, $b)
{
  return strcmp($a["featureDescription"], $b["featureDescription"]);
}

usort($myArray, "myCmp");
+2
source

- array_multisort. , FeatureDescription ( foreach ) .

$featureDescriptionValues = array();    
foreach ($myArray as $node)
{
    $featureDescriptionValues[] = $node['featureDescription'];
}

array_multisort($myArray, $featureDescriptionValues, SORT_STRING, SORT_ASC);

, $featureDescriptionValues , $myArray.

+1

All Articles