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
Jorge
Forum Newbie
Posts: 14 Joined: Tue Mar 21, 2006 8:36 pm
Post
by Jorge » Wed May 30, 2007 5:29 pm
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!
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Wed May 30, 2007 5:33 pm
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 » Wed May 30, 2007 5:45 pm
Yes, thank you, that worked.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed May 30, 2007 5:45 pm
or simply:
Code: Select all
$array = array('dog' => 'The Dog', 'cat' => 'The Cat' );
$array['mouse'] = 'The Mouse';
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Wed May 30, 2007 6:54 pm
I think weirdan's is faster, but I could be wrong.
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Wed May 30, 2007 8:22 pm
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>
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Wed May 30, 2007 8:28 pm
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 » Thu May 31, 2007 12:49 am
Thank you all, I opted for the second answer.