Created
March 18, 2013 06:34
-
-
Save phoenixg/5185420 to your computer and use it in GitHub Desktop.
数组排序吗?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Classify { | |
public $idKey = ''; | |
public $parentIdKey = ''; | |
/** | |
* | |
* @param string $formatStr | |
*/ | |
public function __construct() { | |
} | |
/** | |
* | |
* @param array $rows | |
* @param int $parent_id | |
* @return NULL,array | |
*/ | |
public function classify($array, $parent_id = 0) { | |
$childs = $this->findChild ( $array, $parent_id ); | |
if (empty ( $childs )) { | |
return null; | |
} | |
foreach ( $childs as $k => $v ) { | |
$rescurTree = $this->classify ( $array, $v [$this->idKey] ); | |
if (null != $rescurTree) { | |
$childs [$k] ['childs'] = $rescurTree; | |
} | |
} | |
return $childs; | |
} | |
/** | |
* | |
* @param array $arr | |
* @param int $id | |
* @return NULL,array | |
*/ | |
protected function findChild(&$array, $id) { | |
$childs = array (); | |
foreach ( $array as $k => $v ) { | |
if ($v [$this->parentIdKey] == $id) { | |
$childs [] = $v; | |
} | |
} | |
return $childs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment