array_push in associative array.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

array_push in associative array.

Post by Jorge »

Hello all.

Is there any way I could use array_push to push new key-values in an associative array?
The following is pseudo-code of what I wish to achieve:

Code: Select all

$array = array('dog'  => 'The Dog', 'cat' => 'The Cat' );
array_push($array, 'mouse' => 'The Mouse');
If I can't use array_push to accomplish this then what can I use?

Cheers!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Maybe something like this, haven't tried it but it might work.

Code: Select all

$array = array('dog'  => 'The Dog', 'cat' => 'The Cat' ) + array('mouse' => 'The Mouse');
Edit: It does indeed work, if this is what you are after.

Code: Select all

C:\Users\HP_Administrator>php -r "$array = array('dog' => 'The Dog', 'cat' => 'T
he Cat') + array('mouse' => 'The Mouse');  print_r($array);
Array
(
    [dog] => The Dog
    [cat] => The Cat
    [mouse] => The Mouse
)

C:\Users\HP_Administrator>
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

Yes, thank you, that worked.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or simply:

Code: Select all

$array = array('dog'  => 'The Dog', 'cat' => 'The Cat' );
$array['mouse'] = 'The Mouse';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think weirdan's is faster, but I could be wrong.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Doing a primitive test out of boredom, it appears you're right, Everah.

Code: Select all

C:\Users\HP_Administrator>php -r "$start = microtime(true); for($i=0;$i<1000000;
$i++){ $array = array('dog' => 'The Dog', 'cat' => 'The Cat') + array('mouse' =>
 'The Mouse'); } $end = microtime(true);  echo $end-$start;"

2.1446731090546

C:\Users\HP_Administrator>php -r "$start = microtime(true); for($i=0;$i<1000000;
$i++){ $array = array('dog' => 'The Dog', 'cat' => 'The Cat'); $array['mouse'] =
 'The Mouse'; } $end = microtime(true); echo $end-$start;"

1.2985091209412

C:\Users\HP_Administrator>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, of course. It's an associative array. Weirdan's is faster and simpler, however, if that array element already exists, then it will just replace it.

No idea what your code would do in that case though.
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

Thank you all, I opted for the second answer.
Post Reply