Page 1 of 1

associative array help wanted

Posted: Mon Feb 02, 2009 4:14 am
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.

Re: associative array help wanted

Posted: Mon Feb 02, 2009 4:22 am
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?

Re: associative array help wanted

Posted: Mon Feb 02, 2009 5:39 am
by susrisha
try this

Code: Select all

 
foreach($arrEU as $key1->$key2){
$arrEU[$key1] .= ','.$key2;
}
 

Re: associative array help wanted

Posted: Mon Feb 02, 2009 8:30 am
by indianyogi
@susrisha

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

Thank yo.u

Re: associative array help wanted

Posted: Mon Feb 02, 2009 8:52 am
by Eran
Not sure what was the question exactly, but '->' is the object reference operator and not the array iteration operator which is '=>'