Re: Array Push question

From: Date: Thu, 12 Jul 2007 10:18:24 +0000
Subject: Re: Array Push question
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-258679@lists.php.net to get a copy of this message
Zoltán Németh wrote:
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.
Indeed, my bad. In that case the OP is better off creating the item in a temporary variable and then adding it to the array. $row = array(); $row['test'] = 'this is a test'; $Rows[] = $row; -Stut -- http://stut.net/ >> 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/ >> >

« previous php.general (#258679) next »