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?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.