将元素移动到数组中的第一个位置 [英] Move an element to first position in an array
本文介绍了将元素移动到数组中的第一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的数组:
Array
(
[0] => Array
(
[id] => 8
[title] => MUSIC
)
[1] => Array
(
[id] => 17
[title] => Indie
)
[2] => Array
(
[id] => 14
[title] => SPORTS
)
[3] => Array // Need to move this on first position
(
[id] => 15
[title] => Hipster
)
[4] => Array
(
[id] => 16
[title] => Web Seriesdf
)
)
我希望键[3]的数组位于第一个位置,然后位于其余元素.我尝试了array_merge和array_unshift.但不起作用
I want array with key [3] to be on first position and then the rest of the elements. I tried array_merge and array_unshift. But not working
推荐答案
您只需要执行三个步骤即可.
-将第 n个数组复制到变量中.
-从数组中删除第 n 个索引.使用 unset()
-将变量放置在数组的第0个索引处.使用 array_unshift()
You just need to take only three steps.
- Copy n'th array in variable.
- Delete n'th index from array. using unset()
- Put variable at the 0th index of the array. Using array_unshift()
第1步:
$array=$mainArray[N];
第2步:
unset($mainArray[N]);
第3步:
array_unshift($mainArray, $array);
这篇关于将元素移动到数组中的第一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文