Populate MySql Tables from Multidimensional PHP array

479

So I have a multidimensional array$fillarray and its over 1000 lines long so$fillarray[0] - $fillarray[1000], each element has 22 sections. I'm not concerned with [0][0] on any branch map.

There are several posts regarding pulling from a MySQL table and populating an array, I'm trying to go the other way.

I have tried this several ways. Any help will be greatly appreciated.

for($i = 1; $i <= $cnt; ++$i) {
    if($i != 1)
        $values .= ',(NULL,';
    else
        $values = '(NULL,';
    for($t = 1; $t <= 21; ++$t){
        if($t != 21)
            $value .= "'".$fillarray[$i][$t]."',";
        else
            $values .= "'".$fillarray[$i][$t]."')";
        //echo $fillarray[$i][$t]."<br>";
    if($i != $start)
        $values .= ",";
    }
    mysql_query("insert into `data` VALUES(NULL, '$fillarray[$i][1]' , '$fillarray[$i][2]', '$fillarray[$i][3]', '$fillarray[$i][4]', '$fillarray[$i][5]','$fillarray[$i][6]','$fillarray[$i][7]','$fillarray[$i][8]','$fillarray[$i][9]','$fillarray[$i][10]','$fillarray[$i][11]','$fillarray[$i][12]','$fillarray[$i][13]','$fillarray[$i][14]','$fillarray[$i][15]','$fillarray[$i][16]','$fillarray[$i][17]','$fillarray[$i][18]','$fillarray[$i][19]','$fillarray[$i][20]','$fillarray[$i][21]')") or die(mysql_error());

}
383

Answer

Solution:

try to use foreach loop for multidimensional arra ...

foreach($value as $values){

 echo $values;
}

or

foreach($values as $key=>$values){

echo $key=>$values;


} 
899

Answer

Solution:

Wasn't able to test it, but would something like this work?

foreach ($fillarray as $row) {
    $query = "INSERT INTO `data` VALUES (" . implode(',', array_map('mysql_real_escape_string', array_unshift($row, NULL))) . ")";
}

Also, inb4 "Don't use mysql_*, use PDO/mysqli"!

People are also looking for solutions to the problem: Resumable uploading to Google Cloud Storage using PHP API

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.