PHP:对数组进行排序
作者:互联网
我有一个包含嵌套集模型中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的所有帖子,依此类推.
任何帮助将受到高度赞赏.
先感谢您.
解决方法:
不要在PHP中这样做!
MySQL服务器专门用于查询和排序数据,读取MySQL“ORDER BY”语法.在MySQL服务器上执行此操作将节省运行时,CPU负载和内存消耗.
标签:php,arrays,sorting,nested-sets 来源: https://codeday.me/bug/20190607/1192873.html