Page 1 of 1
Sort an array using another array?
Posted: Thu Oct 12, 2006 11:55 pm
by Stryks
I know this is probrably going to sound odd, but here goes.
I have an array of products that gets passed around in the following format:
$items[item_id] = "product name"
Anyhow .. for most of the time, that is all the data that I need. But under certain circumstances I need to pull a list of what type of item is. At the moment they are types 1 to 5 from an array like this"
$types[item_id] = item_type
So, I want the array holding the products sorted first by it's type, then alphabetically by name.
I'm at a bit of a loss .... any help?
Posted: Fri Oct 13, 2006 1:04 am
by AshrakTheWhite
why dont you just use the same array for both the activities?
Code: Select all
$item['type'] = $type;
$item['name'] = $name;
more code would be helpful to be honest
Re: Sort an array using another array?
Posted: Fri Oct 13, 2006 2:16 am
by Christopher
You probably want to use something like
uasort() and organize you data as a multidimensional array or as an array of objects.
Code: Select all
$items[item_id]['name'] = "product name";
$types[item_id]['type'] = $item_type;
Posted: Fri Oct 13, 2006 10:44 am
by Luke
yea I would set up an array of classes, and then you could give them compare methods:
Code: Select all
<?php
class Item{
var $name;
var $type;
function Item($name, $type){
$this->name = $name;
$this->type = $type;
}
function compareType($that){
if(!is_a($that, 'Item')) exit('Wrong parameter type sent to Item::compareType. Expecting "Item"');
if(strtolower($this->type) == strtolower($that->type)) return 0;
return (strtolower($this->type) > strtolower($that->type)) ? 1 : -1;
}
function compareName($that){
if(!is_a($that, 'Item')) exit('Wrong parameter type sent to Item::compareType. Expecting "Item"');
if(strtolower($this->name) == strtolower($that->name)) return 0;
return (strtolower($this->name) > strtolower($that->name)) ? 1 : -1;
}
function sortType($a, $b){
return $a->compareType($b);
}
function sortName($a, $b){
return $a->compareName($b);
}
}
$names = array('Steve','Mike','Joe','Bob','Ralph','Larry','Dan','Jim','Brian','Luke');
$types = array('Programmer','Programmer','floormat','Cup','book','Something','Pen','Knife','Sand','Backhoe');
$Items = array();
for($i=0; $i<10; $i++){
$Items[] = new Item($names[$i], $types[$i]);
}
usort($Items, array('Item','sortName'));
print_r($Items);
?>
I'm sure this could be elaborated on quite a bit, but this is the easiest way to do that sort of thing in my opinion...
You guys like the completely random array of types?

Posted: Fri Oct 13, 2006 11:03 am
by choppsta
I've always used
array_multisort for this kind of thing. The syntax is a little odd, but it seems to work ok.
Posted: Fri Oct 13, 2006 11:56 am
by pickle
My first reaction would be to use
array_multisort() as well. First off,
sort() your name and type arrays - so they're in the correct order. Then, you can call
array_multisort() like so:
Code: Select all
array_multisort($type_array,$name_array);
array_multisort() is the biggest voodoo function I've come across, but I think that should work.