3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function printPerson($person) { echo sprintf( "Name: %s | Age: %s | Color: %s" . PHP_EOL, $person["name"], $person["age"], $person["colour"] ); } $people = array( array("name"=>"Bob","age"=>8,"colour"=>"red"), array("name"=>"Greg","age"=>12,"colour"=>"blue"), array("name"=>"Andy","age"=>5,"colour"=>"purple") ); $arrSortArray = array(); foreach($people as $person) { foreach($person as $key=>$value){ if(!isset($arrSortArray[$key])){ $arrSortArray[$key] = array(); } $arrSortArray[$key][] = $person; } } $orderby = "name"; //change this to whatever key you want from the array foreach ($arrSortArray[$orderby] as $person) { printPerson($person); } array_multisort($arrSortArray[$orderby], SORT_ASC,$people); foreach ($arrSortArray[$orderby] as $person) { printPerson($person); }
based on rmIAU
Output for 4.3.10 - 4.4.9, 5.0.2 - 7.1.0
Name: Bob | Age: 8 | Color: red Name: Greg | Age: 12 | Color: blue Name: Andy | Age: 5 | Color: purple Name: Andy | Age: 5 | Color: purple Name: Bob | Age: 8 | Color: red Name: Greg | Age: 12 | Color: blue
Output for 4.3.0 - 4.3.9, 5.0.0 - 5.0.1
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/sTl08 on line 5 Name: Bob | Age: 8 | Color: redPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/sTl08 on line 5 Name: Greg | Age: 12 | Color: bluePHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/sTl08 on line 5 Name: Andy | Age: 5 | Color: purplePHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/sTl08 on line 5 Name: Andy | Age: 5 | Color: purplePHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/sTl08 on line 5 Name: Bob | Age: 8 | Color: redPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/sTl08 on line 5 Name: Greg | Age: 12 | Color: bluePHP_EOL