<?php
/**
* @author Prasath A.R
* @copyright 2012
* @Date 2012-8-31 17:14
*/
$array = array (0 => array ( 'id' => '1', 'parent' => '0', ),
1 => array ( 'id' => '2', 'parent' => '1', ),
2 => array ( 'id' => '3', 'parent' => '0', ),
3 => array ( 'id' => '5', 'parent' => '0', ),
4 => array ( 'id' => '17', 'parent' => '3', ),
5 => array ( 'id' => '31', 'parent' => '2', ),
6 => array ( 'id' => '32', 'parent' => '2', ));
print_r($array);
echo "<br />";
for($i=0;$i<count($array);$i++)
{
for($j=$i;$j<count($array);$j++)
{
if($array[$i]['parent']>$array[$j]['parent'])
{
$temp=$array[$i];
$array[$i]=$array[$j];
$array[$j]=$temp;
}
}
}
echo "<h2>After Sorting</h2><br />";
print_r($array);
?>
答案将是:
数组 ( [0] => 数组 ( [id] => 1 [父] => 0 )
[1] => Array
(
[id] => 2
[parent] => 1
)
[2] => Array
(
[id] => 3
[parent] => 0
)
[3] => Array
(
[id] => 5
[parent] => 0
)
[4] => Array
(
[id] => 17
[parent] => 3
)
[5] => Array
(
[id] => 31
[parent] => 2
)
[6] => Array
(
[id] => 32
[parent] => 2
)
)
排序后
数组 ( [0] => 数组 ( [id] => 1 [父] => 0 )
[1] => Array
(
[id] => 3
[parent] => 0
)
[2] => Array
(
[id] => 5
[parent] => 0
)
[3] => Array
(
[id] => 2
[parent] => 1
)
[4] => Array
(
[id] => 31
[parent] => 2
)
[5] => Array
(
[id] => 32
[parent] => 2
)
[6] => Array
(
[id] => 17
[parent] => 3
)
)