note 98465 modified in function.ksort by danbrown

From: Date: Thu, 17 Jun 2010 13:53:46 +0000
Subject: note 98465 modified in function.ksort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170202@lists.php.net to get a copy of this message
A nice way to do sorting of a key on a multi-dimensional array without having to know what keys you have in the array first: <?php $people = array( array("name"=>"Bob","age"=>8,"colour"=>"red"), array("name"=>"Greg","age"=>12,"colour"=>"blue"), array("name"=>"Andy","age"=>5,"colour"=>"purple")); var_dump($people); $sortArray = array(); foreach($people as $person){ foreach($person as $key=>$value){ if(!isset($sortArray[$key])){ $sortArray[$key] = array(); } $sortArray[$key][] = $value; } } $orderby = "name"; //change this to whatever key you want from the array array_multisort($sortArray[$orderby],SORT_DESC,$people); var_dump($people); ?> Output from first var_dump: [0]=&gt; array(3) { ["name"]=&gt; string(3) "Bob" ["age"]=&gt; int(8) ["colour"]=&gt; string(3) "red" } [1]=&gt; array(3) { ["name"]=&gt; string(4) "Greg" ["age"]=&gt; int(12) ["colour"]=&gt; string(4) "blue" } [2]=&gt; array(3) { ["name"]=&gt; string(4) "Andy" ["age"]=&gt; int(5) ["colour"]=&gt; string(6) "purple" } } Output from 2nd var_dump: array(3) { [0]=&gt; array(3) { ["name"]=&gt; string(4) "Greg" ["age"]=&gt; int(12) ["colour"]=&gt; string(4) "blue" } [1]=&gt; array(3) { ["name"]=&gt; string(3) "Bob" ["age"]=&gt; int(8) ["colour"]=&gt; string(3) "red" } [2]=&gt; array(3) { ["name"]=&gt; string(4) "Andy" ["age"]=&gt; int(5) ["colour"]=&gt; string(6) "purple" } There's no checking on whether your array keys exist, or the array data you are searching on is actually there, but easy enough to add. --was-- A nice way to do sorting of a key on a multi-dimensional array without having to know what keys you have in the array first: $people = array( array("name"=>"Bob","age"=>8,"colour"=>"red"), array("name"=>"Greg","age"=>12,"colour"=>"blue"), array("name"=>"Andy","age"=>5,"colour"=>"purple")); var_dump($people); $sortArray = array(); foreach($people as $person){ foreach($person as $key=>$value){ if(!isset($sortArray[$key])){ $sortArray[$key] = array(); } $sortArray[$key][] = $value; } } $orderby = "name"; //change this to whatever key you want from the array array_multisort($sortArray[$orderby],SORT_DESC,$people); var_dump($people); Output from first var_dump: [0]=&gt; array(3) { ["name"]=&gt; string(3) "Bob" ["age"]=&gt; int(8) ["colour"]=&gt; string(3) "red" } [1]=&gt; array(3) { ["name"]=&gt; string(4) "Greg" ["age"]=&gt; int(12) ["colour"]=&gt; string(4) "blue" } [2]=&gt; array(3) { ["name"]=&gt; string(4) "Andy" ["age"]=&gt; int(5) ["colour"]=&gt; string(6) "purple" } } Output from 2nd var_dump: array(3) { [0]=&gt; array(3) { ["name"]=&gt; string(4) "Greg" ["age"]=&gt; int(12) ["colour"]=&gt; string(4) "blue" } [1]=&gt; array(3) { ["name"]=&gt; string(3) "Bob" ["age"]=&gt; int(8) ["colour"]=&gt; string(3) "red" } [2]=&gt; array(3) { ["name"]=&gt; string(4) "Andy" ["age"]=&gt; int(5) ["colour"]=&gt; string(6) "purple" } There's no checking on whether your array keys exist, or the array data you are searching on is actually there, but easy enough to add. http://php.net/manual/en/function.ksort.php

« previous php.notes (#170202) next »