note 39385 added to function.usort
| From: | arjini at mac dot com | Date: | Tue, 27 Jan 2004 03:05:15 +0000 |
| Subject: | note 39385 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64221@lists.php.net to get a copy of this message | ||
<?php
/*
This is a class I built to sort parent/child relationships of array elements.
I post this here, because thanks to usort, you can also assign an explicit order
to the elements.
I first built this to sort categories and subcategories.
My database has four fields. category_id, parent_id, category_name, category_position
$rows is an associative array from the database with my categories.
*/
$rows = array( 0=>array('category_id'=>1,'parent_id'
=>3,'category_name' =>'Second Child','category_position'=>2),
1=>array('category_id' =>2,'parent_id'
=>0,'category_name' =>'Second
Parent','category_position'=>2),
2=>array('category_id' =>3,'parent_id'
=>0,'category_name' =>'First Parent','category_position'=>1),
3=>array('category_id' =>4,'parent_id'
=>0,'category_name' =>'Third Parent','category_position'=>3),
4=>array('category_id' =>5,'parent_id'
=>3,'category_name' =>'First Child','category_position'=>1),
5=>array('category_id' =>6,'parent_id'
=>5,'category_name'=>'Second
Sub-Child','category_position'=>2),
6=>array('category_id' =>7,'parent_id'
=>5,'category_name' =>'First
Sub-Child','category_position'=>1)
);
$ordered = chain('category_id', 'parent_id', 'category_position',
$rows);
foreach($ordered as $item)
{
echo str_repeat('------',
$item['indent']).$item['category_name'].'<br>';
}
/*
Output
First Parent
------First Child
------------First Sub-Child
------------Second Sub-Child
------Second Child
Second Parent
Third Parent
You can have it only return a certain "branch" if you specify a root_id.
Aditionally, you can keep it by going to deep by specifying max-level.
Hope someone finds this useful, I sure would have liked to see this here a week ago.
Ah yes... and the code that runs it.
*/
function chain($primary_field, $parent_field, $sort_field, $rows, $root_id=0, $maxlevel=25)
{
$c = new chain($primary_field, $parent_field, $sort_field, $rows, $root_id, $maxlevel);
return $c->chain_table;
}
class chain
{
var $table;
var $rows;
var $chain_table;
var $primary_field;
var $parent_field;
var $sort_field;
function chain($primary_field, $parent_field, $sort_field, $rows, $root_id, $maxlevel)
{
$this->rows = $rows;
$this->primary_field = $primary_field;
$this->parent_field = $parent_field;
$this->sort_field = $sort_field;
$this->buildChain($root_id,$maxlevel);
}
function buildChain($rootcatid,$maxlevel)
{
foreach($this->rows as $row)
{
$this->table[$row[$this->parent_field]][ $row[$this->primary_field]] = $row;
}
$this->makeBranch($rootcatid,0,$maxlevel);
}
function makeBranch($parent_id,$level,$maxlevel)
{
$rows=$this->table[$parent_id];
foreach($rows as $key=>$value)
{
$rows[$key]['key'] = $this->sort_field;
}
usort($rows,'chainCMP');
foreach($rows as $item)
{
$item['indent'] = $level;
$this->chain_table[] = $item;
if((isset($this->table[$item[$this->primary_field]])) && (($maxlevel>$level+1)
|| ($maxlevel==0)))
{
$this->makeBranch($item[$this->primary_field], $level+1, $maxlevel);
}
}
}
}
function chainCMP($a,$b)
{
if($a[$a['key']] == $b[$b['key']])
{
return 0;
}
return($a[$a['key']]<$b[$b['key']])?-1:1;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.usort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39385
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39385&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39385&report=yes
Search -- http://master.php.net/manage/user-notes.php