Is there a easy way to push into 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Is there a easy way to push into associative array???

Post by raghavan20 »

I have a got an initially defined associative array.
I want to add elements to it (key, value pair) when required.
so looking for an equivalent of array_push() or any easiest way to do it.

ex:
initially
$tempArray = array("john"=>"millers")

after some time I want to add

"peter" => "wright"
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Is there a reason why

Code: Select all

$tempAray["Peter"]="Wright";
wouldn't work?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I was totally unaware that would work.
Since I came from Java, I still dont understand fully the flexibility of PHP arrays and moreover I have not worked so much with associative arrays.
thanks again nielsene.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Might want to look at HashMap instead of array in Java if you want an equivalent for PHP arrays..
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

raghavan20, when you're posting, please make sure(!!!) that you're using

Code: Select all

tags around code.
Post Reply