如何在不重置数组键的情况下最后移位第一个数组元素并生成新的数组


how to shift first array element at last without reset array key and produce new array

这是我的PHP代码,带有out:

<?php
$stack = array( 0 => array("orange", "banana", "apple", "raspberry"),1 => array("banana", "apple", "raspberry"),2 => array("apple", "raspberry"),'a' => array("raspberry"));
$temp = array_keys($stack);
array_shift($temp);
foreach($temp as $item){
    $dd[$item] = $stack[$item];
}
print_r($dd);
?>

输出为:

Array
(
    [1] => Array
        (
            [0] => banana
            [1] => apple
            [2] => raspberry
        )
    [2] => Array
        (
            [0] => apple
            [1] => raspberry
        )
    [a] => Array
        (
            [0] => raspberry
        )
)

但当循环通过I打印数组时,第一个元素丢失了。它应该是数组列表中的最后一个。。。。。

检查此代码。它将在不重置数组键的情况下移位第一个数组并添加最后一个数组:

我希望这能为你工作。。。

<?php
$stack = array( 0 => array("orange", "banana", "apple", "raspberry"),1 => array("banana", "apple", "raspberry"),2 => array("apple", "raspberry"),'a' => array("raspberry"));
$temp = array_keys($stack);
$tem = array_shift($temp);
$temp[] = $tem;
$dd = array();
foreach($temp as $item){
    $dd[$item] = $stack[$item];
}
echo"<pre>"; print_r($dd);
?>

输出:

阵列([1] =>阵列([0]=>香蕉[1] =>苹果[2] =>树莓)

[2] => Array
    (
        [0] => apple
        [1] => raspberry
    )
[a] => Array
    (
        [0] => raspberry
    )
[0] => Array
    (
        [0] => orange
        [1] => banana
        [2] => apple
        [3] => raspberry
    )

)

如果您只是想获得数组中第一个元素的值(而不删除它),请使用reset($stack)

array_shift()将从数组中移除元素并返回值。