gpt4 book ai didi

php - 如何在php中将数组项放在末尾?

转载 作者:可可西里 更新时间:2023-11-01 01:03:16 26 4
gpt4 key购买 nike

如何在php中将一个数组项放到末尾?
我想将一些数组项放在最后一个位置,当我循环并将 $arr 输出到 html 时,这将使“其他”项始终保持在末尾。最好和最简单的方法是什么?

<?php
$arr=array(
'a'=>'hello',
'game'=>'boy',
'other'=>'good',
'name'=>'jimmy',
//...
);


// how to resort $arr to put other item to then end of $arr
$arr=array(
'a'=>'hello',
'game'=>'boy',
'name'=>'jimmy',
//...
'other'=>'good'
);
?>

最佳答案

在您给出的示例中,ksort($arr) 将按字母顺序对其进行排序,并将 other 项放在末尾。

第二个选项是使用 array_slice 从数组中删除 other 项,然后使用 array_merge 将其放在后面

关于php - 如何在php中将数组项放在末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17646451/

26 4 0