[PHP]array_group_by

SQLのGROUP BYの様な処理を配列に行う

public function array_group_by($arr,$keys = array(array('id','address'),array('name') ),$index = 0)
{
    if(empty($arr))
        return null;
    $results = array();
    $children = array();
    $_vkey = $keys[$index][0];
    $_keys = array_slice($keys,0,$index+1);
    $is_final = count($keys) <= $index + 1;
    $id = null;
    array_push($arr,null);
    foreach ($arr as $i => $data){
        $current_id = $this->get_values_by_keys($data,$_keys,'');
        if($id != $current_id){
            if(!is_null($id)) {
                if(!empty($children)) {
                    $tmp = $this->get_std_by_array($children[0], $keys[$index]);
                    $tmp->value = $children[0]->$_vkey;
                }else{
                    $tmp = new stdClass();
                }
                $tmp->children = $is_final ? $children : $this->array_group_by($children,$keys,$index+1);
                $results[] = $tmp;
                $children = array();
            }
            $id = $current_id;
        }
        $children[] = $data;
    }
    return $results;
}
public function get_std_by_array($arr,$include_keys = null)
{
    $tmp = new stdClass();
    foreach ($arr as $key => $v){
        if(empty($include_keys) || (!empty($include_keys) && in_array($key,$include_keys)))
            $tmp->$key = $v;
    }
    return $tmp;
}
public function get_values_by_keys($data,$keys,$glue = null)
{
    if(empty($data))
        return null;
    $tmp = array();
    foreach ($keys as $key){
        if(is_array($key))
            $key = array_shift(array_values($key));
        $tmp[] = $data->$key;
    }
    return is_null($glue) ? $tmp : implode($glue,$tmp);
}