我有一个与此类似的PHP数组:
0 => "red",
1 => "green",
2 => "blue",
3 => "yellow"
Run Code Online (Sandbox Code Playgroud)
我想将黄色移动到索引0.我该怎么做?
编辑:我的问题是如何将这些元素中的任何一个移动到开头?如何将绿色移至索引0,或将蓝色移至索引0?这个问题并不仅仅是将最后一个元素移到开头.
Sha*_*rpC 21
这对我来说似乎是最简单的方法.您可以将任何位置移动到开头,而不仅仅是最后一个位置(在此示例中,它将蓝色移动到开头).
$colours = array("red", "green", "blue", "yellow");
$movecolour = $colours[2];
unset($colours[2]);
array_unshift($colours, $movecolour);
Run Code Online (Sandbox Code Playgroud)
Pet*_*ley 13
可能是最直截了当的方式
array_unshift( $arr, array_pop( $arr ) );
Run Code Online (Sandbox Code Playgroud)
根据你的评论"如何从数组中取出任何一个下标并将其移到开头",我上面的答案并不完全满足该请求 - 它只能通过将最后一个元素移动到0索引来实现.
但是,该功能确实满足了该要求
/**
* Move array element by index. Only works with zero-based,
* contiguously-indexed arrays
*
* @param array $array
* @param integer $from Use NULL when you want to move the last element
* @param integer $to New index for moved element. Use NULL to push
*
* @throws Exception
*
* @return array Newly re-ordered array
*/
function moveValueByIndex( array $array, $from=null, $to=null )
{
if ( null === $from )
{
$from = count( $array ) - 1;
}
if ( !isset( $array[$from] ) )
{
throw new Exception( "Offset $from does not exist" );
}
if ( array_keys( $array ) != range( 0, count( $array ) - 1 ) )
{
throw new Exception( "Invalid array keys" );
}
$value = $array[$from];
unset( $array[$from] );
if ( null === $to )
{
array_push( $array, $value );
} else {
$tail = array_splice( $array, $to );
array_push( $array, $value );
$array = array_merge( $array, $tail );
}
return $array;
}
Run Code Online (Sandbox Code Playgroud)
并且,在使用中
$arr = array( 'red', 'green', 'blue', 'yellow' );
echo implode( ',', $arr ); // red,green,blue,yellow
// Move 'blue' to the beginning
$arr = moveValueByIndex( $arr, 2, 0 );
echo implode( ',', $arr ); // blue,red,green,yellow
Run Code Online (Sandbox Code Playgroud)
此函数允许您将元素移动到数组中的任意位置,同时保持数组的其余部分不变:
function array_reorder($array, $oldIndex, $newIndex) {
array_splice(
$array,
$newIndex,
count($array),
array_merge(
array_splice($array, $oldIndex, 1),
array_slice($array, $newIndex, count($array))
)
);
return $array;
}
Run Code Online (Sandbox Code Playgroud)
希望用法相当明显,所以这个:
$array = array('red','green','blue','yellow',);
var_dump(
array_reorder($array, 3, 0),
array_reorder($array, 0, 3),
array_reorder($array, 1, 3),
array_reorder($array, 2, 0)
);
Run Code Online (Sandbox Code Playgroud)
输出这个:
array(4) {
[0]=>
string(6) "yellow"
[1]=>
string(3) "red"
[2]=>
string(5) "green"
[3]=>
string(4) "blue"
}
array(4) {
[0]=>
string(5) "green"
[1]=>
string(4) "blue"
[2]=>
string(6) "yellow"
[3]=>
string(3) "red"
}
array(4) {
[0]=>
string(3) "red"
[1]=>
string(4) "blue"
[2]=>
string(6) "yellow"
[3]=>
string(5) "green"
}
array(4) {
[0]=>
string(4) "blue"
[1]=>
string(3) "red"
[2]=>
string(5) "green"
[3]=>
string(6) "yellow"
}
Run Code Online (Sandbox Code Playgroud)
您想将其中一个元素移动到开头。比方说
$old = array(
'key1' =>'value1',
'key2' =>'value2',
'key3' =>'value3',
'key4' =>'value4');
Run Code Online (Sandbox Code Playgroud)
并且您想将 key3 移至开头。
$new = array();
$new['key3'] = $old['key3']; // This is the first item of array $new
foreach($old as $key => $value) // This will continue adding $old values but key3
{
if($key != 'key3')$new[$key]=$value;
}
Run Code Online (Sandbox Code Playgroud)
这与 SharpC 的答案非常相似,但考虑到了这样一个事实:您可能不知道该值在数组中的位置(它是键),或者甚至不知道它是否已设置。如果未设置颜色或者它已经是数组中的第一个元素,则“if”检查将跳过它。
$color = 'yellow';
$color_array = array("red", "green", "blue", "yellow");
$key = array_search($color, $color_array);
if ($key > 0) {
unset($color_array[$key]);
array_unshift($color_array, $color);
}
Run Code Online (Sandbox Code Playgroud)
编辑
这是基于问题的更新,并且喜欢 Peter Bailey 答案的一般方面。但是,该代码对我来说功能/内存过于密集,因此下面仅对 $from 和 $to 值进行简单交换。此方法根本不会导致相关数组的大小发生调整,它只是交换到其中的值。
第二次编辑:我添加了一些更多的参数检查,如评论中提到的。
function moveValueByIndex( array $array, $from=null, $to=null )
{
// There is no array, or there are either none or a single entry
if ( null === $array || count($array) < 2 )
{
// Nothing to do, just return what we had
return $array;
}
if ( null === $from )
{
$from = count( $array ) - 1;
}
if ( null === $to )
{
$to = 0;
}
if ( $to == $from )
{
return $array;
}
if ( !array_key_exists($from, $array) )
{
throw new Exception( "Key $from does not exist in supplied array." );
}
$value = $array[$from];
$array[$from] = $array[$to];
$array[$to] = $value;
return $array;
}
Run Code Online (Sandbox Code Playgroud)
如果我应该在 Peter 帖子的评论中添加此内容,请原谅我。将所有这些内容内联起来太长了:/