Reputation: 3972
Im trying to remove the first element of the last array
The array:
$harbours = array(
'67' => array('boat1', 'boat2'),
'43' => array('boat3', 'boat4')
);
I want to remove and return boat3
$last = end($harbours);
$boat = array_shift($last);
If I then print_r ($harbours)
, 'boat3' is still there.
Upvotes: 4
Views: 162
Reputation: 2291
$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));
Upvotes: -1
Reputation: 146310
That is because in array_shift
you are changing a copy of the end array.
You need to get a reference of the end array in order to shift it.
Try this:
end($array);
$currKey = key($array); //get the last key
array_shift($array[$currKey]);
See Demo: http://codepad.org/ey3IVfIL
Upvotes: 10
Reputation: 21979
This code should work as expected:
$harbours = array('67' => array('boat1', 'boat2'), '43' => array('boat3', 'boat4'));
end($harbours);
$key = key($harbours);
$x = $harbours[$key];
array_shift($x);
$harbours[$key] = $x;
print_r($harbours);
Upvotes: -2