Skip to content

Instantly share code, notes, and snippets.

@phoenixg
Created March 18, 2013 06:34
Show Gist options
  • Save phoenixg/5185420 to your computer and use it in GitHub Desktop.
Save phoenixg/5185420 to your computer and use it in GitHub Desktop.
数组排序吗?
<?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