PHP:数组进行排序 [英] PHP: Sort an array

查看:110
本文介绍了PHP:数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与嵌套集模型一个MySQL表数据的数组,我想与孩子的父节点后直接节点得到排序,不仅按字母顺序排列,但也。
示例 - 数组(之前排序)进行排序:

I've got an array with data from a MySQL table in nested set model I'd like to get sorted, not only alphabetical but also with the child nodes directly after the parent node. Example - array to be sorted (before the sorting):

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
        )

)

我想数组这种方式(排序后):

I want the array this way (after the sorting):

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的所有帖子,等等。

As you might see, I want all posts with parent 2 directly after the post with id 2, and so on.

任何帮助将是非常美联社preciated。
谢谢你在前进。

Any help would be highly appreciated. Thank you in advance.

推荐答案

问题解决了 - 我做了两个简单的功能。我希望其他人能有使用这个问题,以及:

Problem solved - I made two simple functions. I hope other people could have use of this as well:

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

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

Thank you for your time. Any improvements are more than welcome.

这篇关于PHP:数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆