0

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

4 Answers 4

1

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!

Sign up to request clarification or add additional context in comments.

4 Comments

+1, though you're missing the emphasis on proper escaping using mysql_real_escape_string
I'd prefer the questioner learn the pertinent things, few things at a time, because he/she seems to be a novice PHP programmer.
I'm a firm believer in immersion; what if you never saw this person again and he would continue writing his queries like that ;-)
Well, thanks to you, if he's up to it he may have looked into what mysql_real_escape_string is.
1

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!

Comments

0

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) . "')";

1 Comment

Good tip, but this is not the biggest problem :)
0

Seems like you've missed one more foreach:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.