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?

2 Answers 2

2

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");
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.