1

Suppose I have an array of arrays, and I want to sort the big array by the # of indexes in the sub-arrays, what would be the most efficient way to do that?

This is what I have so far

$sortorder = array_map('count', $largearray);
array_multisort($sortorder, $largearray);

Anything better than this?

2 Answers 2

3
usort($array, create_function('$a, $b', 'return bccomp(count($a), count($b));'));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, one other thing-- is there a way to update this to make it ignore indexes without values when counting?
1

Below is an answer that will work with PHP8+:

usort($array, function ($a, $b) { return sizeof($b) - sizeof($a); });

1 Comment

thanks for sharing. I posted this question 14 years ago and still getting new stuff on it!

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.