Jump to content

Unexpected behavior: foreach... {$invert['country'][] = $value['country']}


ryandward

Recommended Posts

I am trying to invert some arrays, making the x-values y, and the y-values x. I have found the code that works, but I cannot understand for the life of me,

 

I have a multidimensional $data array; that looks like this:

 

row:0 :: Country:Algeria
row:1 :: Country:USA 
row:2 :: Country:Morocco

 

The following code successfully extracts the appropriate 'column' from my array.

 

foreach($data as $value){
   $invert['country'][] = $value['country'];
}

 

It returns:

 

$invert['country'] = Algeria, USA, Morocco

 

But this makes no sense, it seems more logical that the code should be:

 

foreach($data as $value){
   $invert[] ['country']= $value['country'];
}

 

Since we are accepting all values of the first array, and then specifying the second array.

 

What am I missing here? Why is it working backwards?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.