Jump to content

Multidimensional array to Mysql


devofash

Recommended Posts

Hi,

 

Could really do with some help.

 

So I've got a Multidimensional array like so

 

Array
(
    [Asia] => Array
        (
            [0] => China
            [1] => Japan
        )

    [Europe] => Array
        (
            [0] => France
            [1] => Germany
            [2] => Spain
            [3] => UK
        )
)

 

I have a Mysql table (continents pre-filled)

 

Continent - Country
Asia       
Europe

 

I need to update country where continent is xxx

 

So I need a query/php function which will when run would fill in "china, japan" into "asia" and "france, germany, spain, uk" into "europe", like so

 

Continent - Country
Asia          - China, Japan
Europe     - France, Germany, Spain, UK

 

I don't really know where to start so I don't have a code. Would really appreciate some assistance.

 

 

Link to comment
Share on other sites

Iterate that array in first level (Continent)

Then, you could use implode function in PHP to concatenate array to string

 

foreach ($arr as $continent => $countries) {
$val = implode(", ", $countries); // $val = 'China, Japan'
$sql = "UPDATE table_name SET Country='$val' WHERE Continent='$continent'";
}

Link to comment
Share on other sites

You shouldn't be storing data like this.  You should have:

CONTINENT

-------------

CONTINENT_ID

NAME

 

 

COUNTRY

-------------

COUNTRY_ID

CONTINENT_ID

NAME

 

 

That way, you don't have delimited strings in your database (which is [almost] always wrong) and you'll be able to do real queries on this data.

 

Then, your inserts would be easier too, without having to use implode().

Link to comment
Share on other sites

Iterate that array in first level (Continent)

Then, you could use implode function in PHP to concatenate array to string

 

foreach ($arr as $continent => $countries) {
$val = implode(", ", $countries); // $val = 'China, Japan'
$sql = "UPDATE table_name SET Country='$val' WHERE Continent='$continent'";
}

 

Perfect many thanks.

Link to comment
Share on other sites

You shouldn't be storing data like this.  You should have:

CONTINENT

-------------

CONTINENT_ID

NAME

 

 

COUNTRY

-------------

COUNTRY_ID

CONTINENT_ID

NAME

 

 

That way, you don't have delimited strings in your database (which is [almost] always wrong) and you'll be able to do real queries on this data.

 

Then, your inserts would be easier too, without having to use implode().

 

Thanks. Point noted. But I'm filling in for someone and don't really want to change too much of their code.

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.