简体   繁体   中英

Insert multidimensional array information into database

This seems like it would have a really easy solution...but I've had a hard time figuring it out. I need an array to go into a database. For example:

$usa = array(
'idaho' => array(
           county1 => array(
                     'pocatello', 'arimo', 'downey'
                      ),
           county2 => array(
                      'bear', 'cuprum', 'mesa'
                      )
'iowa' => array(
           county1 => array(
                     'des moines', 'adel', 'desoto'
                      ),
           county2 => array(
                      'douglas', 'grant', 'jasper'
                      )

);

I tried this method of inserting into the database:

foreach($usa as $state => $county){
    foreach($county as $name => $city){
    $s=$state;
    $county_name = strtolower($name);
    $city = strtolower($city);
    $us = "INSERT INTO us
           SET state='{$s}',county='{$county_name}',city='{$city}'
           ";
    $us_result = mysql_query($us,$connection);
         }
  }

I believe the problem is the foreach (passing the state variable into the second foreach loop). I have tried this a number of different ways. Thanks in advance for your help!

***Note: everything works great when I remove the $s=$state variable and the state='{$s}' portion of the insert. I still cant get it to insert the state

There are two major problems.

First, your array isn't delimited properly and it's better to use a single or double quote for the county names, which you are referring to as strings anyway:

$usa = array(
  'idaho' => array(
       'county1'=>array(
                 'pocatello', 'arimo', 'downey'
                  ),
       'county2'=>array(
                  'bear', 'cuprum', 'mesa'
                  )),
  'iowa' => array(
       'county1'=>array(
                 'des moines', 'adel', 'desoto'
                  ),
       'county2'=>array(
                  'douglas', 'grant', 'jasper'
                  ))

);

Secondly, there should be one more foreach loop to account for city names:

foreach($usa as $state => $county){
  foreach($county as $name => $city){
    foreach ($city as $cityname) {
      $s = $state;
      $county_name = strtolower($name);
      $city = strtolower($cityname);
      $us = "INSERT INTO us SET state='{$s}',county='{$county_name}',city='{$city}'";
      echo $us.'<br>';
    }
  }
}

Hope this helps!

First. As @itsmeee stated, you were missing one more foreach that iterates through the array of cities in that county. Besides that, your input array is missing 2 close parens to wrap the array of data per state.

If you fix that I would just do:

$us     = "INSERT INTO us (state, county, city) VALUES ";
$values = array();

foreach($usa as $state => $county){
    foreach($county as $name => $cities){
        foreach($cities as $city){
            $county_name = strtolower($name);
            $city        = strtolower($city);
            $values[]    = "('{$state}','{$county_name}','{$city}')";
        }
    }
}

$us       .= join(',',$values);
$us_result = mysql_query($us,$connection);

This would build the insert string and do all the inserts in one shot. You could also do individual inserts but for a data set this small doing one large insert is more efficient.

Good Luck!

Your INSERT query is incorrect. Try this:

$us = "INSERT INTO us (state, county, city) VALUES ('" . mysql_real_escape_string ($s) . "', '" . mysql_real_escape_string ($county_name) . "', '" . mysql_real_escape_string ($city) . "')";

Seems like you've missed one more foreach:

foreach($county as $name => $cities) {
 foreach ($cities as $city) {
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM