在PHP数组移动值到数组的开头 [英] Move Value in PHP Array to the Beginning of the Array

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

问题描述

我有一个类似的PHP数组:

I have a PHP array similar to this:

0 => "red",
1 => "green",
2 => "blue",
3 => "yellow"

我想搬到黄指数为0。我该怎么办呢?

I want to move yellow to index 0. How do I do this?

编辑:我的问题是我怎么将这些元素开始的任何一个?我怎么会提出绿色指数0,或蓝色索引0?这个问题是不完全有关的最后一个元素移动到开始处。

My question is how do I move any one of these elements to the beginning? How would I move green to index 0, or blue to index 0? This question is not exclusively about moving the last element to the beginning.

推荐答案

这似乎是对我来说最简单的方法。可以在任何位置移动到开始的时候,不只是最后(在这个例子中它移动蓝色到开头)。

This seems like the simplest way to me. You can move any position to the beginning, not just the last (in this example it moves blue to the beginning).

$colours = array("red", "green", "blue", "yellow");

$movecolour = $colours[2];
unset($colours[2]);
array_unshift($colours, $movecolour);

这篇关于在PHP数组移动值到数组的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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