gpt4 book ai didi

PHP array_multisort - 如何保留键值?

转载 作者:可可西里 更新时间:2023-11-01 13:11:57 27 4
gpt4 key购买 nike

<分区>

如何对多维数组进行深度排序并保留它们的键?

$array = [
'2' => [
'title' => 'Flower',
'order' => 3
],
'3' => [
'title' => 'Rock',
'order' => 1
],
'4' => [
'title' => 'Grass',
'order' => 2
]
];

foreach ($array as $key => $row) {
$items[$key] = $row['order'];
}

array_multisort($items, SORT_DESC, $array);

print_r($array);

结果:

Array
(
[0] => Array
(
[title] => Flower
[order] => 3
)

[1] => Array
(
[title] => Grass
[order] => 2
)

[2] => Array
(
[title] => Rock
[order] => 1
)

)

我追求的是:

Array
(
[2] => Array
(
[title] => Flower
[order] => 3
)

[4] => Array
(
[title] => Grass
[order] => 2
)

[3] => Array
(
[title] => Rock
[order] => 1
)

)

有什么想法吗?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com