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
Switch to threaded view of this topic Create a new topic Submit Reply
Last element in an array? [message #169421] Sat, 11 September 2010 19:08 Go to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
I know of the end() function to find the last element in an array, and
I know of the count() or sizeof() function to tell you how many
elements are in an array.

What I can't seem to find is a function that returns the key value of
the last element. Can someone please point me in the right direction?

Thanks.
Re: Last element in an array? [message #169422 is a reply to message #169421] Sat, 11 September 2010 19:32 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
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

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: Last element in an array? [message #169423 is a reply to message #169422] Sat, 11 September 2010 21:35 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
On Sep 11, 2: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
>
> --
> Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
> (Coluche)

lol, OK, thanks.! that's neat.
Re: Last element in an array? [message #169441 is a reply to message #169423] Mon, 13 September 2010 10:44 Go to previous messageGo to next message
Piyush Gupta is currently offline  Piyush Gupta
Messages: 6
Registered: September 2010
Karma: 0
Junior Member
On Sep 12, 2:35 am, MikeB <mpbr...@gmail.com> wrote:
> On Sep 11, 2: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
>
>> --
>> Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
>> (Coluche)
>
> lol, OK, thanks.!  that's neat.

Hi ,

We at www.rntlabs.com [RNTLABS Software Solutions] have exciting
openings for the below

1/ PHP
2/ Designers
3/ Android
4/ iPhone
5/ QA
6/ ROR

Please apply at hr(at)rntlabs(dot)com ASAP .

Thanks

Piyush Gupta
Re: Last element in an array? [message #169448 is a reply to message #169441] Mon, 13 September 2010 15:54 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Piyush Gupta wrote:

>
> Thanks
>
> Piyush Gupta

I'm sorry to reply to spam, but I thought this was the most harmless way
to test if my new newsreader configuration is working.

I suppose there might be "test" newsgroup somewhere, but I'm probably
not subscribed to it.
Re: Last element in an array? [message #169449 is a reply to message #169448] Mon, 13 September 2010 16:46 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/13/2010 11:54 AM, MikeB wrote:
> Piyush Gupta wrote:
>
>>
>> Thanks
>>
>> Piyush Gupta
>
> I'm sorry to reply to spam, but I thought this was the most harmless way
> to test if my new newsreader configuration is working.
>
> I suppose there might be "test" newsgroup somewhere, but I'm probably
> not subscribed to it.
>

Yes, there is a test newsgroup, which is created just for such a purpose.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Last element in an array? [message #169453 is a reply to message #169422] Mon, 13 September 2010 18:14 Go to previous messageGo to next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
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 ??

Thanks.
Re: Last element in an array? [message #169456 is a reply to message #169453] Mon, 13 September 2010 18:56 Go to previous messageGo to next message
Marious Barrier is currently offline  Marious Barrier
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
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.
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 next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
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.
Re: Last element in an array? [message #169459 is a reply to message #169457] Mon, 13 September 2010 21:53 Go to previous messageGo to next message
Marious Barrier is currently offline  Marious Barrier
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
On 09/13/2010 05:34 PM, matt wrote:
> 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.

Its simple, PHP won’t make a real copy in memory when there’s no need.
When you do not pass a variable by reference, the working data will be
the same, like if it was a reference, until you change it.
Try doing it.
Re: Last element in an array? [message #169471 is a reply to message #169457] Tue, 14 September 2010 18:46 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
matt schrieb:

> $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";

[outputs]
> 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.

No. First, you should prepend a $before = memory_get_usage() to your
above code and then subtract $before from the three echoed values to
measure only what you intend to measure. On my system, that gives:

10248
10296
10488

As Marious explained, PHP doesn't actually create a copy unless it
really has to. So when you do $b = $a, no copy is made and both
variables point to the same value stored in memory. Only when you change
$b, the value is actually copied and the change performed on the copy.
But in your example above, you are not changing the 10000 chars string!
When you create the array $a, its first element will simply point to a
location in memory where the long string is stored. Now, when you do the
$b[] = "B", you are simply appending another string to the array, but
you leave its first element -- the long string -- unchanged. If you did
$b[0][0] = "B", then you would observe the expected memory consumption,
because you would change the long string's first character (and thereby
the whole string), so it would have to be copied:

10248
10296
20456

Greetings,
Thomas


--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: Last element in an array? [message #169475 is a reply to message #169471] Tue, 14 September 2010 20:10 Go to previous message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 14, 2:46 pm, Thomas Mlynarczyk <tho...@mlynarczyk-webdesign.de>
wrote:
> matt schrieb:
>
>> $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";
>
> [outputs]
>
>> 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.
>
> No. First, you should prepend a $before = memory_get_usage() to your
> above code and then subtract $before from the three echoed values to
> measure only what you intend to measure. On my system, that gives:
>
> 10248
> 10296
> 10488
>
> As Marious explained, PHP doesn't actually create a copy unless it
> really has to. So when you do $b = $a, no copy is made and both
> variables point to the same value stored in memory. Only when you change
> $b, the value is actually copied and the change performed on the copy.
> But in your example above, you are not changing the 10000 chars string!
> When you create the array $a, its first element will simply point to a
> location in memory where the long string is stored. Now, when you do the
> $b[] = "B", you are simply appending another string to the array, but
> you leave its first element -- the long string -- unchanged. If you did
> $b[0][0] = "B", then you would observe the expected memory consumption,
> because you would change the long string's first character (and thereby
> the whole string), so it would have to be copied:
>
> 10248
> 10296
> 20456
>

Aha! I didn't realize that PHP was actually managing memory down to
individual array elements. That makes my results make a lot more
sense, thanks!
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Sun Nov 24 14:57:56 GMT 2024

Total time taken to generate the page: 0.02741 seconds