PHP - reformat multidimensional array to insert into MYSQL?

后端 未结 4 539
栀梦
栀梦 2020-12-17 07:06

how can I parse php array like this:

$cars= array(
    \"Ford\"=>array(\"C-Max\"=>array(\"length\"=>\"4333\",\"width\"=>\"1825\",\"height\"=>\         


        
4条回答
  •  清歌不尽
    2020-12-17 07:44

    You need two loops, one for the brands and one for their models:

    foreach ($cars as $brand => $models) {
        foreach ($models as $model => $specs) {
            $query = "INSERT INTO cars_demensions (brand, model, length, weight, height)
                      VALUES ('$brand', '$model', {$specs['length']}, {$specs['width']}, {$specs['height']});";
        }
    }
    

提交回复
热议问题