PHP - sort arrays within assoc array


<em>I promise you I've had a look at the many existing SO Qs about PHP sorting, including <a href="https://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php" rel="nofollow">this mega one</a></em>

I've got a PHP associative array, with strings as keys. Each value is an array of integers. I want to sort each array of integers, in simple ascending numerical order. I'm convinced this should be easy, and I've found enough examples that I think I should be doing the right thing, but it's not quite working, so there's a typo or I'm an idiot or something...


//Each fruit corresponds to an array (series) of integers $data = [ 'banana' => [ 1,3,2 ], 'orange' => [ 5,1,3 ] ]; echo "Before sort:\n"; var_dump($data); //For each fruit, I want to order the numbers foreach ($data as $key => $series) { //Sort array of integers sort($series); //NB I wasn't sure about value/reference details of foreach loops, so I also tried //retrieving a series into a variable, sorting, and then reassigning back to the same key } echo "\n\nAfter sort:\n"; var_dump($data);


Before sort: array(2) { 'banana' => array(3) { [0] => int(1) [1] => int(3) [2] => int(2) } 'orange' => array(3) { [0] => int(5) [1] => int(1) [2] => int(3) } } After sort: array(2) { 'banana' => array(3) { [0] => int(1) [1] => int(3) [2] => int(2) } 'orange' => array(3) { [0] => int(5) [1] => int(1) [2] => int(3) } }

As you can see, in the output the inner arrays of integers have not been sorted. What am I doing wrong? (PHP 5.5.9, Windows 7)


Use a reference &:

foreach ($data as $key => &$series) { //Sort array of integers sort($series); // OR // sort($data[$key]); }


  • document.write inside for loops
  • Git reset failing after find and replace
  • DataTables footerCallback - conditional on another cell
  • Server Side Includes vs. ASP.NET Techniques is there a performance difference?
  • Merged iterators produce obscure results
  • Injecting JSP from Spring MVC controller
  • read single line from text file in objective-C
  • Is index used for 'outer' and 'inner' where clauses in nested selects?
  • Adapting diverging stacked bar chart to use general update pattern
  • Using subst in an application would screw up type of the result
  • Javascript 2D array sorting - by numerical value
  • Adding scores to MongoDB aggregation using $switch
  • Available pagefile size / virtual memory
  • R: merging copies of the same variable
  • how access value of array list on Struts framework by properties file
  • Delete object in core data, failed to match the Swift Array Element type
  • android with msaccess
  • Using python to determine if a timestamp is under daylight savings time
  • Find duplicate values in one of the two columns in a text file
  • Ordering a Union Query in MS Access SQL
  • SQL Server - Is there a collation that provides natural order for numbers?
  • How to sort things out in ListView?
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • Getting syntax error in mysql-php. You have an error in your SQL syntax;
  • Android changing fragment order inside FragmentPagerAdapter
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Jquery popup on mouse over of calendar control
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • How do I get HTML corresponding to current DOM tree?
  • Could not find rake using whenever rails
  • How to create a file in java without a extension
  • Breeze - Deleted Items nav properties bug
  • javaw.exe and eclipse startup problems
  • Javascript + PHP Encryption with pidCrypt
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • need help with bizarre java.net.HttpURLConnection behavior
  • Qt: Run a script BEFORE make
  • Conditional In-Line CSS for IE and Others?