tags:

views:

42

answers:

2

I have an array of arrays. The inner array looks like this.

 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 PHPs internal functions?

+1  A: 

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

You probably want usort().

<?php

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

usort($myArray, "myCmp");
Bill Karwin
Thank you very much works perfect
dnaluz
+1  A: 

One way would be to use the array_multisort function. The only downside to this is that you require a copy of all the featureDescription values (with a quick foreach for a example) from your array's first level.

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

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

It is important that the $featureDescriptionValues appear in the same order as they are represented in $myArray.

Denis 'Alpheus' Čahuk