associative array help wanted

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
indianyogi
Forum Newbie
Posts: 7
Joined: Tue Jan 20, 2009 9:16 am

associative array help wanted

Post by indianyogi »

I have an associative array .. something like

Code: Select all

$arrEU=array("Brussels"=>"Peter Hubert ","Paris"=> "France","Berlin"=> "Beyer",....
I want to club all values with key "Brussels" together. For this i am doing something like this:

Code: Select all

foreach($arrEU as $key1->$key2){
$arrEU[$key1] += $arrEU[$key1];
}
is this correct?

how can i add coma ',' between every $key2 values ? .. so that the output is something like

echo ($arrEU["Brussels"]));

Output : Peter Hubert,Beyer,....

Thanks in advance.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: associative array help wanted

Post by requinix »

What the heck?

1) Each key in an associative array is unique. You can't have two identical keys with different values. The array you're describing can't exist (at least as far as I can tell).
2) That code is just an example, right? Because the syntax is wrong, and the code itself doesn't make sense.
3) How did you get Beyer from Brussels?
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: associative array help wanted

Post by susrisha »

try this

Code: Select all

 
foreach($arrEU as $key1->$key2){
$arrEU[$key1] .= ','.$key2;
}
 
indianyogi
Forum Newbie
Posts: 7
Joined: Tue Jan 20, 2009 9:16 am

Re: associative array help wanted

Post by indianyogi »

@susrisha

I think i didnt framed my issues correctly. Nevertheless, u understood. Your code works like a charm!

Thank yo.u
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: associative array help wanted

Post by Eran »

Not sure what was the question exactly, but '->' is the object reference operator and not the array iteration operator which is '=>'
Post Reply