Individual array sorting using string values
Let's say I have this array:
$array = array(
array("id" => 7867867, "animal" => "Dog"),
array("id" => 3452342, "animal" => "Lion"),
array("id" => 1231233, "animal" => "Lion"),
array("id" => 5867867, "animal" => "Dog"),
array("id" => 1111111, "animal" => "Zeebra"),
array("id" => 2222222, "animal" => "Cat"),
array("id" => 3333333, "animal" => "Cat"),
array("id" => 4444444, "animal" => "Zeebra")
);
Now what I was trying to do was use the php sort functions to be able to sort it based on certain rules (not in alphabetical order)
The client wants this information to be sorted "First Lion, Dog Two, Zebra Third, Cat Four."
Something like that:
$array = array(
array("id" => 3452342, "animal" => "Lion"),
array("id" => 1231233, "animal" => "Lion"),
array("id" => 7867867, "animal" => "Dog"),
array("id" => 5867867, "animal" => "Dog"),
array("id" => 4444444, "animal" => "Zeebra"),
array("id" => 1111111, "animal" => "Zeebra"),
array("id" => 2222222, "animal" => "Cat"),
array("id" => 3333333, "animal" => "Cat"),
);
The array will be sorted using the value "animal" and will be based on predefined rules.
I was trying to figure out php sorting functions, but I could only get them to work with sorting arrays alphabetically or numerically.
What I got is a block of if-statements and loops, and I would like to get rid of this slow code as soon as possible.
usort, , :
function getAnimalValue($animal) {
switch($animal) {
case 'Lion':
return 1;
case 'Dog':
return 2;
case 'Zeebra':
return 3;
case 'Cat':
return 4;
}
return 0;
}
:
function compare($itemA, $itemB) {
$a = getAnimalValue($itemA['animal']);
$b = getAnimalValue($itemB['animal']);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
, usort :
usort($array, "compare");
usort. :
http://www.php.net/manual/en/function.usort.php
:
function cmp($a, $b) {
$order=array("Lion","Dog","Zebra","Cat");
if ($a["animal"] == $b["animal"]) {
return 0;
}
return (array_search($a["animal"],$order) < array_search($b["animal"],$order)) ? -1 : 1;
}
$array = array(
array("id" => 7867867, "animal" => "Dog"),
array("id" => 3452342, "animal" => "Lion"),
array("id" => 1231233, "animal" => "Lion"),
array("id" => 5867867, "animal" => "Dog"),
array("id" => 1111111, "animal" => "Zebra"),
array("id" => 2222222, "animal" => "Cat"),
array("id" => 3333333, "animal" => "Cat"),
array("id" => 4444444, "animal" => "Zebra")
);
$mySortedArray=usort($array, "cmp");
This is a bit ugly way, but it works.
// Set our rules, the key is the order
$rules = array(0=>"Dog",1=>"Lion",2=>"Zeebra",3=>"Cat");
// Our array
$array = array(
array("id" => 7867867, "animal" => "Dog"),
array("id" => 3452342, "animal" => "Lion"),
array("id" => 1231233, "animal" => "Lion"),
array("id" => 5867867, "animal" => "Dog"),
array("id" => 1111111, "animal" => "Zeebra"),
array("id" => 2222222, "animal" => "Cat"),
array("id" => 3333333, "animal" => "Cat"),
array("id" => 4444444, "animal" => "Zeebra")
);
// Split each animal into a arrays per animal
foreach( $array as $item ){
$animals[ $item['animal'] ][] = $item;
}
// Loop our rules (dogs, then lions, etc) and put our mini arrays in a final sorted_array.
foreach( $rules as $animal_order => $animal_name ){
foreach( $animals[$animal_name] as $animal ){
$sorted_animals[] = $animal;
}
}
print_r($sorted_animals);
Try:
function sortByAnimals($array, $animalOrderArray){
foreach($animalOrderArray as $v){
$aa[$v] = array();
}
foreach($array as $a){
foreach($aa as $i => $v){
if(preg_match("/^{$a['animal']}$/i", $i)){
array_push($aa[$i], $a);
break;
}
}
}
foreach($aa as $a){
foreach($a as $v){
$r[] = $v;
}
}
return $r;
}
$resArray = sortByAnimals($array, array('lion', 'dog', 'zeebra', 'cat'));