Re: [PHP] Array Push question

2007-07-13 Thread Richard Lynch
On Thu, July 12, 2007 3:45 am, John Comerford wrote:
 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'.

count() is not slow, really...

PHP maintains the count internally as you add stuff, so it is a quick
lookup.

Or you could just do:

$Rows[] = array('test'=this is a test);

At least I think that works out the same array-wise...

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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



[PHP] Array Push question

2007-07-12 Thread John Comerford

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'.

TIA,
 JC

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



Re: [PHP] Array Push question

2007-07-12 Thread dev
On Thu, 12 Jul 2007 18:45:36 +1000, John Comerford [EMAIL PROTECTED] 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'.
 
 TIA,
   JC
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php

This code looks weird...

$currenRow will be numeric. So why would you do $currentRow['test']?
Anyway if you just wanna add an element to the end of the array use array_push

http://nl3.php.net/manual/en/function.array-push.php

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



Re: [PHP] Array Push question

2007-07-12 Thread Stut

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.


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/

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



Re: [PHP] Array Push question

2007-07-12 Thread Thijs Lensselink
On Thu, 12 Jul 2007 18:45:36 +1000, John Comerford [EMAIL PROTECTED] 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'.
 
 TIA,
   JC
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php

Seems my eyes are to tired today. Didn't read the code very good.
But the answer still stays the same :)

$Rows = array();
array_push($Rows, array('test' = 'this is a test'));

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



Re: [PHP] Array Push question

2007-07-12 Thread Zoltán Németh
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/
 

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



Re: [PHP] Array Push question

2007-07-12 Thread Stut

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/





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



Re: [PHP] Array Push question

2007-07-12 Thread M. Sokolewicz

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');
?

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



Re: [PHP] Array Push question

2007-07-12 Thread Stut

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



Re: [PHP] Array Push question

2007-07-12 Thread Jim Lucas

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;


I imagine that you are doing other actions between the above three lines? 
Correct?

Really, the simplest way of doing this is to call this over and over again.

$Rows[]['test'] = this is a test;

it will give you the same results as array_push, but without a function call

$Rows[]['test'] = this is a test;
$Rows[]['test'] = this is another test;
$Rows[]['test'] = this is even another test;

print_r($Rows);

this will show you that it does what you are doing in the above code, but 
again, the question.
What are you doing between all the lines of code that made you think that you had to do it your 
example way?  Are you using count() and doing math with it?  or something else?




Specifically I am wonder if I can avoid having to use 'count'.

TIA,
 JC




--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

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



Re: [PHP] Array Push question

2007-07-12 Thread John Comerford
As you guys can probably guess from the question I'm still getting to 
grips with some parts of php.


I had stripped down the code for my original post, maybe this will clear 
things up a bit:


$Rows[] = array();
$currentRow = count($Rows) - 1;
$Rows[$currentRow]['test'] = this is a test;
$Rows[$currentRow]['Data'] = array();
$Rows[$currentRow]['Config'] = This is the row config;

So from what you guys have said I have changed it to be the following:

$Rows[] = array('test' = 'this is a test', 'Data' = array(), 'Config' 
= 'This is the row config');


Which seems to be doing what I want.

Thanks for the replies,
 JC


Jim Lucas wrote:

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;


I imagine that you are doing other actions between the above three 
lines? Correct?


Really, the simplest way of doing this is to call this over and over 
again.


$Rows[]['test'] = this is a test;

it will give you the same results as array_push, but without a 
function call


$Rows[]['test'] = this is a test;
$Rows[]['test'] = this is another test;
$Rows[]['test'] = this is even another test;

print_r($Rows);

this will show you that it does what you are doing in the above code, 
but again, the question.
What are you doing between all the lines of code that made you think 
that you had to do it your example way?  Are you using count() and 
doing math with it?  or something else?




Specifically I am wonder if I can avoid having to use 'count'.

TIA,
 JC



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