문제

중첩 된 세트 모델의 MySQL 테이블의 데이터가 포함 된 배열이 있습니다. 알파벳순뿐만 아니라 부모 노드 직후에 자식 노드와도 정렬되고 싶습니다. 예제 - 정렬 할 배열 (정렬 전에) :

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Kompetenser
            [parent] => 0
            [depth] => 0
        )

    [1] => Array
        (
            [id] => 2
            [name] => Administration
            [parent] => 1
            [depth] => 1
        )

    [2] => Array
        (
            [id] => 11
            [name] => Organisation
            [parent] => 2
            [depth] => 2
        )

    [3] => Array
        (
            [id] => 4
            [name] => Arbetsledning
            [parent] => 2
            [depth] => 2
        )

    [4] => Array
        (
            [id] => 17
            [name] => Planering
            [parent] => 2
            [depth] => 2
        )

    [5] => Array
        (
            [id] => 9
            [name] => Hantverke
            [parent] => 1
            [depth] => 1
        )

    [6] => Array
        (
            [id] => 10
            [name] => Snickeri
            [parent] => 9
            [depth] => 2
        )

    [7] => Array
        (
            [id] => 12
            [name] => Språk
            [parent] => 1
            [depth] => 1
        )

    [8] => Array
        (
            [id] => 13
            [name] => Tolk
            [parent] => 12
            [depth] => 2
        )

    [9] => Array
        (
            [id] => 15
            [name] => Arabiska
            [parent] => 13
            [depth] => 3
        )

    [10] => Array
        (
            [id] => 14
            [name] => Persiska
            [parent] => 13
            [depth] => 3
        )

    [11] => Array
        (
            [id] => 16
            [name] => Polska
            [parent] => 13
            [depth] => 3
        )

    [12] => Array
        (
            [id] => 18
            [name] => Apotekare
            [parent] => 1
            [depth] => 1
        )

    [13] => Array
        (
            [id] => 19
            [name] => Dotkorand
            [parent] => 1
            [depth] => 1
        )

    [14] => Array
        (
            [id] => 21
            [name] => Atomfysik
            [parent] => 19
            [depth] => 2
        )

    [15] => Array
        (
            [id] => 20
            [name] => Fysik
            [parent] => 19
            [depth] => 2
        )

    [16] => Array
        (
            [id] => 22
            [name] => Ekonom
            [parent] => 1
            [depth] => 1
        )

    [17] => Array
        (
            [id] => 23
            [name] => Industriell ekonomi
            [parent] => 22
            [depth] => 2
        )

    [18] => Array
        (
            [id] => 24
            [name] => Filosofi
            [parent] => 1
            [depth] => 1
        )

)

나는이 방법으로 배열을 원한다 (정렬 후) :

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Kompetenser
            [parent] => 0
            [depth] => 0
        )

    [1] => Array
        (
            [id] => 2
            [name] => Administration
            [parent] => 1
            [depth] => 1
        )

    [3] => Array
        (
            [id] => 4
            [name] => Arbetsledning
            [parent] => 2
            [depth] => 2
        )

    [2] => Array
        (
            [id] => 11
            [name] => Organisation
            [parent] => 2
            [depth] => 2
        )

    [4] => Array
        (
            [id] => 17
            [name] => Planering
            [parent] => 2
            [depth] => 2
        )

    [12] => Array
        (
            [id] => 18
            [name] => Apotekare
            [parent] => 1
            [depth] => 1
        )

    [13] => Array
        (
            [id] => 19
            [name] => Dotkorand
            [parent] => 1
            [depth] => 1
        )

    [14] => Array
        (
            [id] => 21
            [name] => Atomfysik
            [parent] => 19
            [depth] => 2
        )

    [15] => Array
        (
            [id] => 20
            [name] => Fysik
            [parent] => 19
            [depth] => 2
        )

    [16] => Array
        (
            [id] => 22
            [name] => Ekonom
            [parent] => 1
            [depth] => 1
        )

    [17] => Array
        (
            [id] => 23
            [name] => Industriell ekonomi
            [parent] => 22
            [depth] => 2
        )

    [18] => Array
        (
            [id] => 24
            [name] => Filosofi
            [parent] => 1
            [depth] => 1
        )

    [5] => Array
        (
            [id] => 9
            [name] => Hantverke
            [parent] => 1
            [depth] => 1
        )

    [6] => Array
        (
            [id] => 10
            [name] => Snickeri
            [parent] => 9
            [depth] => 2
        )

    [7] => Array
        (
            [id] => 12
            [name] => Språk
            [parent] => 1
            [depth] => 1
        )

    [8] => Array
        (
            [id] => 13
            [name] => Tolk
            [parent] => 12
            [depth] => 2
        )

    [9] => Array
        (
            [id] => 15
            [name] => Arabiska
            [parent] => 13
            [depth] => 3
        )


    [10] => Array
        (
            [id] => 14
            [name] => Persiska
            [parent] => 13
            [depth] => 3
        )

    [11] => Array
        (
            [id] => 16
            [name] => Polska
            [parent] => 13
            [depth] => 3
        )

)

보시다시피, ID 2가있는 게시물 직후에 부모 2가있는 모든 게시물을 원합니다.

모든 도움은 높이 평가 될 것입니다. 미리 감사드립니다.

도움이 되었습니까?

해결책 4

문제 해결 - 나는 두 가지 간단한 기능을 만들었습니다. 다른 사람들도 이것을 사용할 수 있기를 바랍니다.

class data_comp
{
    var $fetched_tree = array();

    function tree_fetch($parent = 0)
    {
        $query = 'SELECT node.id, node.name, node.parent, (COUNT(parent.name) - 1) AS depth FROM test_competence AS node, test_competence AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.name ORDER BY node.name';
        $result = mysql_query($query) or die(mysql_error());
        $tree = array();

        while($data = mysql_fetch_assoc($result))
        {
            $tree[$data['parent']][$data['id']] = array('name' => $data['name'], 'depth' => $data['depth']);
        }

        $this->tree_print($tree, $parent);
    }

    function tree_print($tree, $parent)
    {
        foreach($tree[$parent] as $id => $value)
        {
            $this->fetched_tree[] = array('id' => $id, 'name' => $value['name'], 'depth' => $value['depth']);

            if(isset($tree[$id]) && is_array($tree[$id]))
            {
                $this->tree_print($tree, $id);
            }
        }
    }
}

시간 내 주셔서 감사합니다. 모든 개선은 환영받는 것 이상입니다.

다른 팁

PHP에서 이것을하지 마십시오!

MySQL 서버는 데이터를 쿼리하고 정렬하도록 구체적으로 설계되었습니다.주문"구문. MySQL 서버 에서이 작업을 수행하면 런타임, CPU로드 및 메모리 소비가 절약됩니다.

PHP를 사용하십시오 uasort() 자신의 비교 함수를 정의하는 기능.

그러나 MySQL의 정렬 기능을 사용하는 것이 가능하다면 더 적절할 것입니다.

다층 DB처럼 분류하고 싶습니다. 나는 UASORT 가이 문제에 도움이 될 수 있다고 생각하지 않습니다. 왜냐하면 당신은 아이들을 부모에게 첨부하고 싶기 때문입니다.

foreach ($arr as &$val) {
    $arr2[$val['id']] = &$val;
}

ksort($arr2);

foreach ($arr2 as $id => &$val) {
    $parent = $val['parent'];
    if ($parent == 0) {
        continue;
    }
    $arr2[$parent]['children'][$id] = &$val;
}

function flattenArrayByChildren($arr) {
    foreach ($arr as $id => $val) {
        if (isset($val['children'])) {
            $temp = flattenArrayByChildren($val['children']);
            unset($val['children']);
            $out[$id] = $val;
            $out = $out + $temp;
        } else {
            $out[$id] = $val;
        }
    }
    return $out;
}

$arr2 = array(1 => $arr2[1]);

$out = flattenArrayByChildren($arr2);

var_dump($out);

키를 절대적으로 저장하려면 첫 번째 foreach의 $ val에 추가하고 재귀 함수 FlattenarrayChildren에서 검색하고 키로 사용 할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top