FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Last element in an array?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Last element in an array? [message #169457 is a reply to message #169456] Mon, 13 September 2010 21:34 Go to previous messageGo to previous message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma:
Member
On Sep 13, 2:56 pm, Marious Barrier <marious.barr...@gmail.com> wrote:
> On 09/13/2010 02:14 PM, matt wrote:
>
>
>
>> On Sep 11, 3:32 pm, Thomas Mlynarczyk<tho...@mlynarczyk-webdesign.de>
>> wrote:
>>> MikeB schrieb:
>
>>>> I know of the end() function to find the last element in an array [....]
>>>> What I can't seem to find is a function that returns the key value of
>>>> the last element.
>
>>> function lastKey( $array )
>>> {
>>>       end( $array );
>>>       return key( $array );
>
>>> }
>
>>> Greetings,
>>> Thomas
>
>> I'm curious.  Will that affect the internal array pointer in all
>> scopes? ie,
>
>> $array = array(0, 1, 2);
>> next($array);  // internal pointer ->  1
>> lastKey($array);
>> echo current($array); // internal pointer ->  2 ??
>
> Try it to be sure.
> It should not... when not passing an array by reference, you are working
> with a “copy” of the array and its internal pointer.

Right...I believe that however, if you do this:

$a = array(str_repeat("A", 10000));
echo memory_get_usage() . "\n";
$b = $a; // a full copy of a has NOT been made yet
echo memory_get_usage() . "\n";
$b[] = "B"; // now it has, I believe
echo memory_get_usage() . "\n";

Because PHP is doing some sort of smart memory management here that I
don't fully understand. My results for this actually don't seem to
line up with my understanding of how and when PHP copies arrays. I
got the following:

328436
328504
328752

I would have expected the third number to be almost double the first
based on my understanding. If PHP were making a copy right at the
assignment operator, then I'd expect to see the 2nd and 3rd be double
the first.

Yet, the question remains, do the array pointer functions trigger PHP
to make a full copy of the array, or at least maintain separate
pointers to the same memory space?

$a = array(0, 1, 2);
$b = $a;
next($b);
printf("%d, %d\n", current($a), current($b));

yields: "0,1"

So, the answer seems that yes, that does seem to be enough to trigger
PHP to make a full copy, or that $a and $b maintain separate
pointers. Perhaps someone with a deeper knowledge of the internals
could help convince me that I am interpreting these results correctly.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Something I have never seen before
Next Topic: getting amount of deleted rows from: $result=odbc_exec($conn, 'delete from....');
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Oct 21 03:26:03 GMT 2024

Total time taken to generate the page: 0.04138 seconds