Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Sorting Array Keys

I do NOT understand why php would sort "name 3" before "name". It makes no sense please help

<?php

$example = array(

array("name" => "Alejandro"), array("name 2" => "Alejandro"), array("name 3" => "Alejandro"), array("name 4" => "Alejandro"),

);

asort($example);

var_dump($example);

?>

1 Answer

Ryan Lovett
Ryan Lovett
5,100 Points

Keep in mind that asort() sorts the array by the value, not the key. More documentation about asort() can be found here:

http://php.net/manual/en/function.asort.php

To sort by key, you will want to use ksort() instead which you can read about here:

http://php.net/manual/en/function.ksort.php

Hope this helps!

Thank You!