Bluehost.com Web Hosting $6.95

Return value of 'each', 'current', 'next', 'end'

This is a discussion on Return value of 'each', 'current', 'next', 'end' within the PHP Language forums, part of the PHP Programming Forums category; These array functions: 'each', 'current', 'next', 'end' They return a reference or a value. let's say I want to ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-14-2006
Hermann.Richter@gmail.com
 
Posts: n/a
Default Return value of 'each', 'current', 'next', 'end'

These array functions: 'each', 'current', 'next', 'end'

They return a reference or a value.

let's say I want to modify the last value of an array without iterating
through all of them.

I would do:

<?
end($array) ;
current($array)="new value" ;
?>

is that posible??

Thanks.

Reply With Quote
  #2 (permalink)  
Old 10-14-2006
Chung Leong
 
Posts: n/a
Default Re: Return value of 'each', 'current', 'next', 'end'

Hermann.Richter@gmail.com wrote:
> These array functions: 'each', 'current', 'next', 'end'
>
> They return a reference or a value.


They return values.

> let's say I want to modify the last value of an array without iterating
> through all of them.
>
> I would do:
>
> <?
> end($array) ;
> current($array)="new value" ;
> ?>
>
> is that posible??


Try

end($array);
$array[key($array)] = "new value";

Reply With Quote
  #3 (permalink)  
Old 10-14-2006
Daz
 
Posts: n/a
Default Re: Return value of 'each', 'current', 'next', 'end'


Chung Leong wrote:
> Hermann.Richter@gmail.com wrote:
> > These array functions: 'each', 'current', 'next', 'end'
> >
> > They return a reference or a value.

>
> They return values.
>
> > let's say I want to modify the last value of an array without iterating
> > through all of them.
> >
> > I would do:
> >
> > <?
> > end($array) ;
> > current($array)="new value" ;
> > ?>
> >
> > is that posible??

>
> Try
>
> end($array);
> $array[key($array)] = "new value";


$array[(sizeof($array)-1)] ="new value";
should also work.

I would imagine that end() and sizeof() work very much the same way.
They probably both iterate through the array to the end only 1 counts
as it goes, and the other returns the key of the last element in the
array.

Reply With Quote
  #4 (permalink)  
Old 10-15-2006
Pedro Graca
 
Posts: n/a
Default Re: Return value of 'each', 'current', 'next', 'end'

Daz wrote:
>
> Chung Leong wrote:
>> Hermann.Richter@gmail.com wrote:
>> > <?
>> > end($array) ;
>> > current($array)="new value" ;
>> > ?>
>> >
>> > is that posible??

>>
>> end($array);
>> $array[key($array)] = "new value";

>
> $array[(sizeof($array)-1)] ="new value";
> should also work.


<?php
$tmp = array('forty-two'=>42, 'twenty-four'=>24);
echo $array[sizeof($array)-1];
?>

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Reply With Quote
  #5 (permalink)  
Old 10-15-2006
Chung Leong
 
Posts: n/a
Default Re: Return value of 'each', 'current', 'next', 'end'

Daz wrote:
> $array[(sizeof($array)-1)] ="new value";
> should also work.
>
> I would imagine that end() and sizeof() work very much the same way.
> They probably both iterate through the array to the end only 1 counts
> as it goes, and the other returns the key of the last element in the
> array.


No. end() move the internal point inside the array to the last element.
sizeof() returns the number of elements inside the array. As Pedro
illustrated, the distinction is most obvious when you have an
associative array.

Arrays in PHP are hybrid linked-lists and hash tables. Each elements
links to its neighbors and can simultanously be quickly accessed
through a hash key.

Reply With Quote
  #6 (permalink)  
Old 10-16-2006
Daz
 
Posts: n/a
Default Re: Return value of 'each', 'current', 'next', 'end'


Chung Leong wrote:
> Daz wrote:
> > $array[(sizeof($array)-1)] ="new value";
> > should also work.
> >
> > I would imagine that end() and sizeof() work very much the same way.
> > They probably both iterate through the array to the end only 1 counts
> > as it goes, and the other returns the key of the last element in the
> > array.

>
> No. end() move the internal point inside the array to the last element.
> sizeof() returns the number of elements inside the array. As Pedro
> illustrated, the distinction is most obvious when you have an
> associative array.
>
> Arrays in PHP are hybrid linked-lists and hash tables. Each elements
> links to its neighbors and can simultanously be quickly accessed
> through a hash key.


Completely correct of course. I have no idea why I didn't think of that
one. I tend to go out of my way whenever possible to create
non-assiciative arrays when I can, for the reason I illistrated.
Perhaps it's a bad habit, but it has served me well in the past under
certain circumstances. I think this is why associative arrays
completely slipped my mind.

Thanks for clearing that up, and my humble apologies for any confusion
caused to the OP.

All the best.

Daz.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:28 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0