Connecting Tech Pros Worldwide Forums | Help | Site Map

Re-initialize An Array

SamMan
Guest
 
Posts: n/a
#1: Jul 17 '05
Is there an easy way to re-initialize an array, other than looping through
it's length and setting the values to null, or ""? Would unset() be a wise
choice?

Thanks again

--


SamMan
Rip it to reply



Pjotr Wedersteers
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Re-initialize An Array


SamMan wrote:[color=blue]
> Is there an easy way to re-initialize an array, other than looping
> through it's length and setting the values to null, or ""? Would
> unset() be a wise choice?
>
> Thanks again[/color]
Array_fill ?


steve
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Re-initialize An Array


"Pjotr Wedersteers" wrote:[color=blue]
> SamMan wrote:[color=green]
> > Is there an easy way to re-initialize an array, other than[/color][/color]
looping[color=blue][color=green]
> > through it’s length and setting the values to null, or ""?[/color]
> Would[color=green]
> > unset() be a wise choice?
> >
> > Thanks again[/color]
> Array_fill ?[/color]

If you want it to still be an "array" after initialization, do:
$a = array();

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-initiali...ict133056.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=444411
SamMan
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Re-initialize An Array


"steve" <UseLinkToEmail@dbForumz.com> wrote in message
news:10g8b80gp3bec61@news.supernews.com...[color=blue]
> "Pjotr Wedersteers" wrote:[color=green]
> > SamMan wrote:[color=darkred]
> > > Is there an easy way to re-initialize an array, other than[/color][/color]
> looping[color=green][color=darkred]
> > > through it's length and setting the values to null, or ""?[/color]
> > Would[color=darkred]
> > > unset() be a wise choice?
> > >
> > > Thanks again[/color]
> > Array_fill ?[/color]
>
> If you want it to still be an "array" after initialization, do:
> $a = array();
>
> --[/color]


Thanks!


--


SamMan
Rip it to reply


steve
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Re-initialize An Array


"SamMan" wrote:[color=blue]
> "steve" <UseLinkToEmail@dbForumz.com> wrote in message
> news:10g8b80gp3bec61@news.supernews.com...[color=green]
> > "Pjotr Wedersteers" wrote:[color=darkred]
> > > SamMan wrote:
> > > > Is there an easy way to re-initialize an array, other[/color][/color]
> than[color=green]
> > looping[color=darkred]
> > > > through it’s length and setting the values to[/color][/color]
> null, or ""?[color=green][color=darkred]
> > > Would
> > > > unset() be a wise choice?
> > > >
> > > > Thanks again
> > > Array_fill ?[/color]
> >
> > If you want it to still be an "array" after initialization, do:
> > $a = array();
> >
> > --[/color]
>
>
> Thanks!
>
>[/color]

Sam,
I make it a practice to initialize variables that are going to be
array with array(). This way, you are sure not to run into erros
where arrays are expected. For example, there are many functions (e.g.
count(..)) that require arrays. If you simply unset(..), and then
don’t build an array, those functions would give you an error. But if
you do $a=array(), and then not populate the array $a with any values,
and then do a count($a), you still get a value (0), and no errors.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-initiali...ict133056.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=444434
AJ
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Re-initialize An Array



"steve" <UseLinkToEmail@dbForumz.com> wrote in message
news:10g8b80gp3bec61@news.supernews.com...[color=blue]
> "Pjotr Wedersteers" wrote:[color=green]
> > SamMan wrote:[color=darkred]
> > > Is there an easy way to re-initialize an array, other than[/color][/color]
> looping[color=green][color=darkred]
> > > through it's length and setting the values to null, or ""?[/color]
> > Would[color=darkred]
> > > unset() be a wise choice?
> > >
> > > Thanks again[/color]
> > Array_fill ?[/color]
>
> If you want it to still be an "array" after initialization, do:
> $a = array();[/color]

I posted a question a while ago about this and am wondering if it will solve
my problem as well. It's for a shopping cart system that I wrote. What
happens is that the cart is stored in a table while the visitor is filling
it up. The current cart is displayed at the top right of the screen. When
the visitor clicks on checkout, I wanted to carry on displaying the current
cart AND have the full contents in the main body of the page. The problem
was that the array had already reached the end so it wouldn't work and just
displayed nothing the second time around.

Is this the same issue as this poster and will the answer be good for me?

Cheers

Andy


steve
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Re-initialize An Array


"AJ" wrote:[color=blue]
> "steve" <UseLinkToEmail@dbForumz.com> wrote in message
> news:10g8b80gp3bec61@news.supernews.com...[color=green]
> > "Pjotr Wedersteers" wrote:[color=darkred]
> > > SamMan wrote:
> > > > Is there an easy way to re-initialize an array, other[/color][/color]
> than[color=green]
> > looping[color=darkred]
> > > > through it’s length and setting the values to[/color][/color]
> null, or ""?[color=green][color=darkred]
> > > Would
> > > > unset() be a wise choice?
> > > >
> > > > Thanks again
> > > Array_fill ?[/color]
> >
> > If you want it to still be an "array" after initialization, do:
> > $a = array();[/color]
>
> I posted a question a while ago about this and am wondering if it[/color]
will[color=blue]
> solve
> my problem as well. It’s for a shopping cart system that I
> wrote. What
> happens is that the cart is stored in a table while the visitor is
> filling
> it up. The current cart is displayed at the top right of the[/color]
screen.[color=blue]
> When
> the visitor clicks on checkout, I wanted to carry on displaying the
> current
> cart AND have the full contents in the main body of the page. The
> problem
> was that the array had already reached the end so it wouldn’t
> work and just
> displayed nothing the second time around.
>
> Is this the same issue as this poster and will the answer be good[/color]
for[color=blue]
> me?
>
> Cheers
>
> Andy[/color]

Hard to tell, AJ. If the 2nd time around you are showing a new page,
then everything is already initialized. Perhaps you can give it a
try, and post a simplified version of your code.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-initiali...ict133056.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=445075
Closed Thread