how can I parse php array like this:
$cars= array(
\"Ford\"=>array(\"C-Max\"=>array(\"length\"=>\"4333\",\"width\"=>\"1825\",\"height\"=>\
Full executable code and TESTED. This is more efficient and faster way to do this. Using this way you can insert multiple row using a single insert query.
$models) {
foreach ($models as $model => $specs) {
if (isset($specs['length']) || isset($specs['width']) || isset($specs['height'])) {
$length = $specs['length'];
$width = $specs['width'];
$height = $specs['height'];
} else {
foreach ($specs as $k => $v) {
$length = $v['length'];
$width = $v['width'];
$height = $v['height'];
}
}
$col[] = "('" . $brand . "', '" . $model . "', '" . $length . "', '" . $width . "', '" . $height . "')";
}
}
$query = "INSERT INTO `cars_dimensions`(`brand`, `model`, `length`, `width`, `height`) VALUES" . implode(',', $col) . ";";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec($query);
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
Hopefully it should help you. Thank you.