(forgot to CC internals on the previous email)

On Sat, Jul 14, 2012 at 4:42 PM, John LeSueur <john.lesu...@gmail.com> wrote:
> Readability is solved by creating a userland function:
>
> function array_last_key(&$array)
> {
>     return key(array_slice($array, -1,1,true));
> }

Another userland function, with a by-ref parameter. Again more
unnecessary complexity, solved by our new friend array_last_key.

I hope this gets put into a branch soon, I'll happily +1 vote it.

- Paul.

>
> $lastkey = array_last_key($array);
>
> Now, it may still be quicker, or use less memory(temporarily) if it were a
> core function, but I'd like to see numbers before making a decision. I would
> still want to see a significant advantage before it is added to core.
>
>
> On Sat, Jul 14, 2012 at 9:33 AM, Paul Dragoonis <dragoo...@gmail.com> wrote:
>>
>> $lastKey = key(array_slice($array,-1,1,true));
>> vs
>> $lastKey = array_last_key($array);
>>
>> The latter will be quicker, use less memory and easier to read/maintain.
>>
>> - Paul.
>>
>> On Sat, Jul 14, 2012 at 4:28 PM, John LeSueur <john.lesu...@gmail.com>
>> wrote:
>> >
>> >
>> > On Sat, Jul 14, 2012 at 4:34 AM, Paul Dragoonis <dragoo...@gmail.com>
>> > wrote:
>> >>
>> >> On Sat, Jul 14, 2012 at 3:04 AM, Anthony Ferrara <ircmax...@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
>
>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to