$value = reset($arr); $key = key($arr) (in that order) See reset() PHP Manual key() PHP Manual unset($arr$key); # in case you want to remove it However array_pop() PHP Manual is working with the last element: $value = end($arr); $key = key($arr); unset($arr$key); # in case you want to remove it See end() PHP Manual For the fun: list($value, $key) = array(end($arr), key($arr)) or extract(array('value'=>end($arr), 'key'=>key($arr))) or end($arr); list($key, $value) = each($arr) or whatever style of play you like ;) Dealing with empty arrays It was missing so far to deal with empty arrays. So it's a need to check if there is a last (first) element and if not, set the $key to null (as null can not be an array key): for($key=null;$key===null&&false! ==$value=end($arr);) unset($arr$key=key($arr)) This will give for a filled array like $arr = array('first' => '1st', 'last' => '2nd.') : string(4) "2nd.
" # value string(4) "last" # key array(1) { # leftover array "first"=> string(3) "1st" } And an empty array: bool(false) # value NULL # key array(0) { # leftover array } Afraid of using unset? In case you don't trust unset() having the performance you need (of which I don't think it's really an issue, albeit I haven't run any metrics), you can use the native array_pop() implementation as well (but I really think that unset() as a language construct might be even faster): end($arr); $key = key($arr); $value = array_pop($arr).
$value = reset($arr); $key = key($arr); (in that order) See reset()PHP Manual, key()PHP Manual. Unset($arr$key); # in case you want to remove it. However array_pop()PHP Manual is working with the last element: $value = end($arr); $key = key($arr); unset($arr$key); # in case you want to remove it.
See end()PHP Manual. For the fun: list($value, $key) = array(end($arr), key($arr)); or extract(array('value'=>end($arr), 'key'=>key($arr))); or end($arr); list($key, $value) = each($arr); or whatever style of play you like ;) Dealing with empty arrays It was missing so far to deal with empty arrays. So it's a need to check if there is a last (first) element and if not, set the $key to null (as null can not be an array key): for($key=null;$key===null&&false!
==$value=end($arr);) unset($arr$key=key($arr)); This will give for a filled array like $arr = array('first' => '1st', 'last' => '2nd. ');: string(4) "2nd. " # value string(4) "last" # key array(1) { # leftover array "first"=> string(3) "1st" } And an empty array: bool(false) # value NULL # key array(0) { # leftover array } Afraid of using unset?
In case you don't trust unset() having the performance you need (of which I don't think it's really an issue, albeit I haven't run any metrics), you can use the native array_pop() implementation as well (but I really think that unset() as a language construct might be even faster): end($arr); $key = key($arr); $value = array_pop($arr).
Thanks. I am not convinced by: unset($arr$key); It looks like it would require the system to go through the array again to find the actual value. – Pietro Speroni Jul 7 at 10:25 1 An array in PHP is a hash.
That's a lookup operation not a search through the whole array and it is fast. – hakre Jul 7 at 10:29 thanks for the clarification – Pietro Speroni Jul 7 at 10:30 I love the each! – Pietro Speroni Jul 7 at 10:49 Yeah, I like it, too.In case you need to deal with empty arrays as well, it's getting a bit tricky for a compact form, so for the fun you find another edit.
– hakre Jul 7 at 10:55.
Array_slice $arr = array('k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3'); $a = array_slice($arr, 0, 1); var_dump($a); $arr = array_slice($arr, 1); var_dump($arr); array(1) { "k1"=> string(2) "v1" } array(2) { "k2"=> string(2) "v2" "k3"=> string(2) "v3" }.
1 array_slice() is a nice idea. Depending in what should be done with the key and value: list($key, $value) = array_slice($arr, 0, 1); – KingCrunch Jul 7 at 10:33 array_slice() is rather slow, isn't it? I mean it's a nice function but as far as popping from the end of the array is concerned... – hakre Jul 7 at 10:41.
List($value, $key) = array(reset($s), key($s)); array_shift($s); // or just unset($s$key); Of course you can split the first statement into two separate.
– Pietro Speroni Jul 7 at 10:29 1 Yes. Thats what I meant with "you can split it" :) (as long as the array-pointer points to the first element (thats what reset() does), – KingCrunch Jul 7 at 10:31 Unfortunately every time I use array_shift, the array comes out empty. Independently if I use a reset before.
I think I'll use an unset, as suggested above. – Pietro Speroni Jul 7 at 10:48.
$value = reset($array); $key = key($array); Edit: Hakre just beat me to it :-).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.