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?
Sort an array using another array?
Moderator: General Moderators
-
AshrakTheWhite
- Forum Commoner
- Posts: 69
- Joined: Thu Feb 02, 2006 6:47 am
why dont you just use the same array for both the activities?
more code would be helpful to be honest
Code: Select all
$item['type'] = $type;
$item['name'] = $name;- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Sort an array using another array?
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;(#10850)
yea I would set up an array of classes, and then you could give them compare methods:
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?
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);
?>You guys like the completely random array of types?
I've always used array_multisort for this kind of thing. The syntax is a little odd, but it seems to work ok.
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:
array_multisort() is the biggest voodoo function I've come across, but I think that should work.
Code: Select all
array_multisort($type_array,$name_array);Real programmers don't comment their code. If it was hard to write, it should be hard to understand.