array_shift来自数组中的数组

时间:2012-12-19 13:42:22

标签: php arrays

我试图删除最后一个数组的第一个元素

数组:

$harbours = array(
              '67' => array('boat1', 'boat2'),
              '43' => array('boat3', 'boat4')
            );

我想删除并返回boat3

$last = end($harbours);
$boat = array_shift($last);

如果我然后print_r ($harbours),'boat3'仍在那里。

2 个答案:

答案 0 :(得分:10)

这是因为在array_shift您正在更改结束数组的副本

您需要获取结束数组的引用才能将其移位。

试试这个:

end($array);

$currKey = key($array); //get the last key

array_shift($array[$currKey]);

请参阅演示:http://codepad.org/ey3IVfIL

答案 1 :(得分:-1)

$last = end($harbours);<br />

//First reverse the array and then pop the last element, which will be the first of original array.<br />
array_pop(array_reverse($last));