我有一个类似于以下PHP数组:
0 => "red",
1 => "green",
2 => "blue",
3 => "yellow"
我想把黄色移动到索引0。我该怎么做?
编辑:我的问题是如何将任何一个元素移动到开头?我该如何将绿色或蓝色移动到索引0?这个问题不仅仅关于将最后一个元素移动到开头。
对我来说,这似乎是最简单的方法。您可以将任何位置移到开头,而不仅仅是最后一个(在此示例中,它将蓝色移动到开头)。
$colours = array("red", "green", "blue", "yellow");
$movecolour = $colours[2];
unset($colours[2]);
array_unshift($colours, $movecolour);
可能是最直接的方法。
array_unshift( $arr, array_pop( $arr ) );
根据您的评论“我如何从数组中取任何一个下标并将其移动到开头”,我上面的回答并没有完全满足该要求 - 它仅通过将最后一个元素移动到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;
}
而且,在使用时
$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
这个函数可以让你将一个元素移动到数组中任意位置,同时保持数组其他元素不变:
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;
}
希望使用方法很明显,因此这样做:
$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)
);
将输出这个:
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"
}
$old = array(
'key1' =>'value1',
'key2' =>'value2',
'key3' =>'value3',
'key4' =>'value4');
您希望将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;
}
这与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);
}
$colors = array("red","green","blue","yellow");
$color_to_move = ["yellow"];
$colors_wo_yellow = array_diff($colors, $color_to_move);// This will give an array without "yellow"
//Now add "yellow" as 1st element to $
array_unshift($colors_wo_yellow,$color_to_move[0]);
编辑:
这是基于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;
}
如果我应该在Peter的帖子中添加评论,那么请原谅我..只是太长了,不能全部内联显示 :/
抱歉,这段内容与IT技术无关。请提供更多相关信息以便我为您提供更准确的翻译。
$array = array(
'red',
'green',
'blue',
'yellow',
);
$last = array_pop($array);
array_unshift($array, $last);
$a = array('red','green', 'blue','yellow');
$b = array_reverse( $a );
$array = array("red", "green", "blue", "yellow");
$last = array_pop($array);
array_unshift($array, $last);
print_r($array);
结果:
Array ( [0] => yellow [1] => red [2] => green [3] => blue )
黄色,红色,绿色,蓝色
,黄色,蓝色,绿色,红色
,黄色,绿色,蓝色,红色
,每个都有不同的处理过程。此外,它是否总是要从末尾移到前面,还是可以是任意位置移到前面? - Reese Moore