Hello,

I am building a multidimensional array with the following code:

$array_two = array(); 
$arrayIndex=0; 
$loopCount=0; 
$second_index=""; 
foreach ($result as $myValue) { 
$loopCount = ($loopCount<5) ? $loopCount : 0; 
switch ($loopCount) { 
case '0': 
$second_index="cName"; 
break; 
case '1': 
$second_index="local"; 
break; 
case '2': 
$second_index="days_times"; 
break; 
case '3': 
$second_index="contact"; 
break; 

case '4': 
$second_index="marker"; 
break; 

} 
$array_two[$arrayIndex][$second_index]=$myValue; 
$arrayIndex = ($loopCount>=4) ? $arrayIndex+1 : $arrayIndex; 
$loopCount++; 
}  

the output is something like:

 Array 
( 
    [0] => Array 
        ( 
            [cName] => Fairbanks Interior Table Tennis - FITT 
(4/30/2014) 
            [local] => Patty Center 
Univ. of Alaska Campus 
Fairbanks, AK  99775 
            [days_times] => Sunday - 3:00 to 5:45PM & Tuesday - 6:00 - 8:30PM & some Thursday evenings 
            [contact] => Diann Darnall 
907-479-5421 
            [marker] => <-----CLUB DATA ENDS -----> 
        ) 
 [1] => Array 
        ( 
            [cName] => Greenville Table Tennis Club 
    (3/31/2014) 
            [local] => Pavilion Greenville Recreation Center 
    400 Scottwood Rd. 
    Taylors, SC  29687 
            [days_times] => Wednesday - 7:00 to 10:00 PM 
            [contact] => David Blair 
    864-915-3993 
            [marker] => <-----CLUB DATA ENDS -----> 
        ) 

)  

Now I am attempting to store the array in mysql with the following:

  $array_two=Array
(
    [0] => Array
        (
            [cName] => Fairbanks Interior Table Tennis - FITT
(4/30/2014)
            [local] => Patty Center
Univ. of Alaska Campus
Fairbanks, AK  99775
            [days_times] => Sunday - 3:00 to 5:45PM & Tuesday - 6:00 - 8:30PM & some Thursday evenings
            [contact] => Diann Darnall
907-479-5421
            [marker] => <-----CLUB DATA ENDS ----->
        )

 [1] => Array
        (
            [cName] => Green Mountain Table Tennis Club(GMTTC)
(6/30/2014)
            [local] => Knights of Columbus - Boys & Girls Club Gymnasium
21 Merchants Row
Rutland, VT  05701
            [days_times] => Wednesday - 6:00 - 10:00PM and 2 Fridays(6:00 - 10:00PM) & 2 Saturdays(7:00AM - 9:00PM) per year for tournaments(last Fri/Sat in March & Fri/Sat in Nov or Dec)
            [contact] => Ronald Lewis
802-247-5913
            [marker] => <-----CLUB DATA ENDS ----->
        )

)
;  

//the sql:

   $c = connect();// my db connection function
    foreach ($array_two as $k => $v) //error LINE 133 
{
        echo $query = "INSERT INTO Clubs_test (id,".
                implode(',',array_keys($v)).
                ") VALUES ($k,".
                implode(',',$v).
            ")";
        $r = mysql_query($query,$c);
    }
    mysql_close($c);  

I am experiencing the following error:

 Warning: Invalid argument supplied for foreach() in /home5/********/public_html/******/********.php on line 133  

I would appreciate some thoughts on how to correct the issue!

Thanks

Recommended Answers

All 2 Replies

The loops seem to be fine, the error means that $array_two is served as a string not as an array. Check if there is an intermediate step that resets the variable.

solved!

Solution is as follows:

///* Working   
   foreach ($array_two as $k => $v) 
{
//$query = "INSERT INTO Clubs_test (".implode(',',array_keys($v)).") VALUES (". implode(',',$v).")";

$r = mysql_query($query,$c) or die(mysql_error());
    }
    mysql_close($c);  

//*/ //Working
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.