Hi there,
I'm proposing a function called array_last_key(), that takes a
reference to an array, and returns the key of its last item. This is
useful for finding out the key of the last item pushed to an array.
Here's the pull request: https://github.com/php/php-src/pull/131
This is the first PHP extension function, and PHP test I've written.
--
Andrew Faulds (AJF)
http://ajf.me/
Changed, it now takes just an array.
Function signature:
mixed array_last_key(array $array)
Hi there,
I'm proposing a function called array_last_key(), that takes a
reference to an array, and returns the key of its last item. This is
useful for finding out the key of the last item pushed to an array.
Here's the pull request: https://github.com/php/php-src/pull/131This is the first PHP extension function, and PHP test I've written.
--
Andrew Faulds (AJF)
http://ajf.me/
--
Andrew Faulds (AJF)
http://ajf.me/
I like this idea. array_first_key would be nice too
On Fri, Jul 13, 2012 at 7:45 PM, Andrew Faulds ajfweb@googlemail.comwrote:
Changed, it now takes just an array.
Function signature:
mixed array_last_key(array $array)
Hi there,
I'm proposing a function called array_last_key(), that takes a
reference to an array, and returns the key of its last item. This is
useful for finding out the key of the last item pushed to an array.
Here's the pull request: https://github.com/php/php-src/pull/131This is the first PHP extension function, and PHP test I've written.
--
Andrew Faulds (AJF)
http://ajf.me/--
Andrew Faulds (AJF)
http://ajf.me/--
--
Brandon Wamboldt
Programmer / Web Developer
StackOverflow Careers
Profilehttp://careers.stackoverflow.com/brandonwamboldt- GitHub
Profile https://github.com/brandonwamboldt -
LinkedInhttps://github.com/brandonwamboldt -
My Blog <http://brandonwamboldt.ca/
Hi!
I like this idea. array_first_key would be nice too
I am probably missing something, but what those would allow to do that
rewind/end+key() doesn't do?
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Yeah, it would allow you to do this in one function call instead of three.
If you are going to have an array_last_key() function, it makes sense to
also have an array_first_key() function. After all, you could just do this:
foreach ( $array as $key => $v ) {}
echo $key;
On Fri, Jul 13, 2012 at 9:37 PM, Stas Malyshev smalyshev@sugarcrm.comwrote:
Hi!
I like this idea. array_first_key would be nice too
I am probably missing something, but what those would allow to do that
rewind/end+key() doesn't do?--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
--
Brandon Wamboldt
Programmer / Web Developer
StackOverflow Careers
Profilehttp://careers.stackoverflow.com/brandonwamboldt- GitHub
Profile https://github.com/brandonwamboldt -
LinkedInhttps://github.com/brandonwamboldt -
My Blog <http://brandonwamboldt.ca/
Stas,
I like this idea. array_first_key would be nice too
I am probably missing something, but what those would allow to do that
rewind/end+key() doesn't do?
The big thing that it does that end()
+key() doesn't do is really what it
doesn't do (update the internal pointer).
end()
modifies the array pointer. So if you try this in user-land:
function array_last_key(array $array) {
end($array);
return key($array);
}
It will always force a full copy of the array (at least the hash table).
Implementing this as a core function however would eliminate that one
side-effect...
Stas,
I like this idea. array_first_key would be nice too
I am probably missing something, but what those would allow to do that
rewind/end+key() doesn't do?The big thing that it does that
end()
+key() doesn't do is really what it
doesn't do (update the internal pointer).
end()
modifies the array pointer. So if you try this in user-land:function array_last_key(array $array) {
end($array);
return key($array);
}It will always force a full copy of the array (at least the hash table).
Implementing this as a core function however would eliminate that one
side-effect...
and it beats doing $var = null; if(isset($array[0])) { $var = $array[0]; }
On Sat, Jul 14, 2012 at 3:04 AM, Anthony Ferrara ircmaxell@gmail.com
wrote:Stas,
I like this idea. array_first_key would be nice too
I am probably missing something, but what those would allow to do that
rewind/end+key() doesn't do?The big thing that it does that
end()
+key() doesn't do is really what it
doesn't do (update the internal pointer).
end()
modifies the array pointer. So if you try this in user-land:function array_last_key(array $array) {
end($array);
return key($array);
}It will always force a full copy of the array (at least the hash table).
Implementing this as a core function however would eliminate that one
side-effect...and it beats doing $var = null; if(isset($array[0])) { $var = $array[0]; }
Not sure that does the trick(you're pulling values, not keys, and assuming
0-based array), but...
What about reset(array_keys($array)); or end(array_keys($array)); which
only makes copies of the keys?
or even better key(array_slice($array, 0, 1, true)); or
key(array_slice($array,-1,1,true)); which makes a copy of one element of
the array?
I'm of the opinion that we mostly have enough array_* functions, and new
ones have to provide a significant advantage to be accepted. If these
functions are significantly faster than the alternatives, then perhaps they
have merit, but they seem like convenience functions, rather than
fundamentals.
John
Em Sat, 14 Jul 2012 04:04:27 +0200, Anthony Ferrara ircmaxell@gmail.com
escreveu:
I like this idea. array_first_key would be nice too
I am probably missing something, but what those would allow to do that
rewind/end+key() doesn't do?The big thing that it does that
end()
+key() doesn't do is really what it
doesn't do (update the internal pointer).
end()
modifies the array pointer. So if you try this in user-land:function array_last_key(array $array) {
end($array);
return key($array);
}It will always force a full copy of the array (at least the hash table).
Implementing this as a core function however would eliminate that one
side-effect...
Why is the last key special? Why not a function to get the first or the
penultimate key?
Of course, there is one aspect where the first and last keys are special
-- if you have some algorithm where you pop or shift the array
successively. But in that case the argument about end()
+ key()
having
side effects is irrelevant because you are changing the array anyway.
--
Gustavo Lopes
Gustavo,
Why is the last key special? Why not a function to get the first or the
penultimate key?
How would such a function look?
Of course, there is one aspect where the first and last keys are special
-- if you have some algorithm where you pop or shift the array
successively. But in that case the argument aboutend()
+key()
having side
effects is irrelevant because you are changing the array anyway.
How are you changing the array by getting the last (or first) key in this
method? Could you elaborate on that?
Anthony
Gustavo,
Why is the last key special? Why not a function to get the first or the
penultimate key?
How would such a function look?
I think the function would look just like the function we have for doing
this.
key(array_slice($arr,-2));
And no, this has no side-effect of changing the array pointer in $arr,
of course.
And array_last_key() is identical to array_slice($arr,-1) is it not?
The only thing array_last_key() saves is a single function call to
key()
. That doesn't seem worth it to me.
-Rasmus
Gustavo,
Why is the last key special? Why not a function to get the first or the
penultimate key?
How would such a function look?
I think the function would look just like the function we have for doing
this.key(array_slice($arr,-2));
And no, this has no side-effect of changing the array pointer in $arr,
of course.And array_last_key() is identical to array_slice($arr,-1) is it not?
The only thing array_last_key() saves is a single function call to
key()
. That doesn't seem worth it to me.
Ok, with the slight modification that for numeric keys we would want to
preserve the keys.
function array_last_key($arr) {
return key(array_slice($arr,-1,null,true));
}
And despite what the docs say, the array pointer is not changed.
Test:
$a = [ 'first'=>1, 'second'=>2, 'third'=>3, 'last'=>4 ];
next($a);
next($a);
echo key($a);
echo array_last_key($a);
echo key($a);
This outputs:
third
last
third
-Rasmus
Em Sat, 14 Jul 2012 19:13:32 +0200, Anthony Ferrara ircmaxell@gmail.com
escreveu:
Why is the last key special? Why not a function to get the first or the
penultimate key?
How would such a function look?
I think you've misread my statement. I was not suggesting a function that
would get an arbitrary key. I was asking what was special about the last
key that would merit a dedicated function. The underlying position was
that there was nothing special about them (but see below), and a dedicated
function function was not warranted.
Of course, there is one aspect where the first and last keys are special
-- if you have some algorithm where you pop or shift the array
successively. But in that case the argument aboutend()
+key()
having
side effects is irrelevant because you are changing the array anyway.How are you changing the array by getting the last (or first) key in this
method? Could you elaborate on that?
I'm afraid I also wasn't clear here. One certainly does not have to change
the array to get the first or the last key. I was only stating that the
circumstances where it would be important to get specifically the first or
the last key are also those where you would want to have side effects. My
argument was that getting specifically the first or last keys is only
important if you are somehow traversing the array by removing the first or
last elements specifically and that requires side effects*.
*This statement requires some qualification. Of course, you don't need
side effects to remove the last element; you could use array_slice()
to
implement a sort of array_rest() or array_most() that would remove the
first or the last element. But 1) it is not efficient to use these
patterns in PHP and 2) since you are anyway copying parts you the array,
it is not a problem to change those copies.
--
Gustavo Lopes