Re: Array Push question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



M. Sokolewicz wrote:
Stut wrote:
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

what's wrong with skipping the temporary variable and just doing
$Rows[] = array('test'=>'this is a test');
?

Nothing, unless you're building a complex element. I was assuming that the OP had simplified their code down to the minimum required to illustrate the problem.

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux