F Q F Q - 2 months ago 6
MySQL Question

how to get array record from a multidimensional array and save it in db

How I can get each array record from a multidimensional array, print it and save it in mysql db with for or foreach loop, the size of array is unknown also.

Array (
[33] => Array ( [name] => A5 [status] => Busy [statusid] => 3 [num] => 33 )
[39] => Array ( [name] => A2 [status] => Available [statusid] => 1 [num] => 39 )
[55] => Array ( [name] => A3 [status] => Busy [statusid] => 3 [num] => 55 )
[54] => Array ( [name] => A4 [status] => Available [statusid] => 1 [num] => 54 )
)

Answer

Size of array doesn't matter, you can use foreach()

$query = "INSERT INTO table (name,status,statusid,num) VALUES ";
$data = array();
foreach ($YourArray as $key => $value) {
   $data[] = "('".$value['name']."','".$value['status']."','".$value['statusid']."','".$value['num']."')";
}
$query .= implode(",",$data);
echo $query;
Comments