Sort an array using another array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Sort an array using another array?

Post 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?
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post 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
User avatar
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?

Post 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;
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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? :lol:
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply