Re: Array Push question
| From: | Zoltán Németh | Date: | Thu, 12 Jul 2007 10:13:27 +0000 |
| Subject: | Re: Array Push question | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-258678@lists.php.net to get a copy of this message | ||
2007. 07. 12, csütörtök keltezéssel 10.28-kor Stut ezt írta:
> John Comerford wrote:
> > Hi Folks,
> >
> > Is there a better way of doing the following:
> >
> > $Rows[] = array();
> > $currentRow = count($Rows) - 1;
> > $Rows[$currentRow]['test'] = "this is a test";
> >
> > Specifically I am wonder if I can avoid having to use 'count'.
>
> 1) The code above will produce a $currentRow of -1 which is probably not
> what you want.
Maybe you didn't notice the [] in the first line of the OP's code?
I think this code should produce 0 for $currentRow, not -1.
greets
Zoltán Németh
>
> 2) If you actually mean that you have an array that contains items and
> you want the last one, used end (http://php.net/end).
>
> 3) I'm not really sure what the context is, but this is a very odd way
> of working with arrays. Either you already know the key or you don't. If
> you're just trying to append to the array you can do that with the
> following syntax... $Rows[] = array('test' => 'this is a test');
>
> -Stut
>
> --
> http://stut.net/
>