PHP:对数组进行排序

时间:2009-05-17 08:52:48

标签: php arrays sorting nested-sets

我在嵌套集模型中有一个包含来自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
        )

)

正如您可能看到的那样,我希望所有帖子都与父2一起在帖子后面的ID为2,依此类推。

任何帮助都将受到高度赞赏。 提前谢谢。

4 个答案:

答案 0 :(得分:4)

不要在PHP中执行此操作!

MySQL服务器专门用于查询和排序数据,读取MySQL“ORDER BY”语法。在MySQL服务器上执行此操作将节省运行时,CPU负载和内存消耗。

答案 1 :(得分:2)

使用php的uasort()函数定义自己的比较函数。

但是如果在你的情况下可以使用MySQL的排序功能会更合适。

答案 2 :(得分:0)

您希望对它进行排序,就像在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,并在递归函数flattenArrayByChildren中检索它并用作密钥。

答案 3 :(得分:0)

问题解决了 - 我做了两个简单的功能。我希望其他人也可以使用它:

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);
            }
        }
    }
}

感谢您的时间。任何改进都非常受欢迎。