我很难循环遍历以下数组 -
array("newProduct"=>array('rmsId' => '123456789','departmentName' => 'Britax','categoryName' => 'Car Seat','productName' => 'Test Product For Britax Car Seat','productPrice' => 33.99,'productQty' => 10),
"newProduct"=>array('rmsId' => '987654321','departmentName' => 'Stokke','categoryName' => 'Highchairs','productName' => 'Test Product For Stokke Highchair','productPrice' => 83.99,'productQty' => 20),
"newProduct"=>array('rmsId' => '13579','departmentName' => 'Maclaren','categoryName' => 'Buggy','productName' => 'Test Product For Maclaren Buggy','productPrice' => 183.99,'productQty' => 5)
);
有人可以让我走上正确的道路来实现这一目标吗?我的目标是将每个添加到MySQL数据库
由于
答案 0 :(得分:3)
你的数组无效;一个密钥只能存在一次。您应该创建数字索引,然后使用foreach()
循环遍历它。
答案 1 :(得分:1)
首先,您需要为每个数组键指定唯一标识符。然后你可以遍历它们。
$items = array(
"product1"=>array('rmsId' => '123456789','departmentName' => 'Britax','categoryName' => 'Car Seat','productName' => 'Test Product For Britax Car Seat','productPrice' => 33.99,'productQty' => 10),
"product2"=>array('rmsId' => '987654321','departmentName' => 'Stokke','categoryName' => 'Highchairs','productName' => 'Test Product For Stokke Highchair','productPrice' => 83.99,'productQty' => 20),
"product3"=>array('rmsId' => '13579','departmentName' => 'Maclaren','categoryName' => 'Buggy','productName' => 'Test Product For Maclaren Buggy','productPrice' => 183.99,'productQty' => 5)
);
foreach($items as $product) {
echo $product['rmsId'];
echo $product['departmentName'];
// ... and so on
}
答案 2 :(得分:1)
试试这个: -
<?php
$var = array("0"=>array('rmsId' => '123456789','departmentName' => 'Britax','categoryName' => 'Car Seat','productName' => 'Test Product For Britax Car Seat','productPrice' => 33.99,'productQty' => 10),
"1"=>array('rmsId' => '987654321','departmentName' => 'Stokke','categoryName' => 'Highchairs','productName' => 'Test Product For Stokke Highchair','productPrice' => 83.99,'productQty' => 20),
"2"=>array('rmsId' => '13579','departmentName' => 'Maclaren','categoryName' => 'Buggy','productName' => 'Test Product For Maclaren Buggy','productPrice' => 183.99,'productQty' => 5)
);
foreach ($var as $key=>$val) {
$id = $val['rmsId'];
$dname = $val['departmentName'];
$cname = $val['categoryName'];
$pname = $val['productName'];
$price = $val['productPrice'];
$qnt = $val['productQty'];
$query = mysql_query("INSERT INTO TABLENAME (rmsId,departmentName,categoryName,productName,productPrice,productQty) VALUE(".$id.",".$dname.",".$cname.",".$pname.",".$price.",".$qnt.")");
}
?>
答案 3 :(得分:1)
$array = array(array('rmsId' => '123456789','departmentName' => 'Britax','categoryName' => 'Car Seat','productName' => 'Test Product For Britax Car Seat','productPrice' => 33.99,'productQty' => 10),
array('rmsId' => '987654321','departmentName' => 'Stokke','categoryName' => 'Highchairs','productName' => 'Test Product For Stokke Highchair','productPrice' => 83.99,'productQty' => 20),
array('rmsId' => '13579','departmentName' => 'Maclaren','categoryName' => 'Buggy','productName' => 'Test Product For Maclaren Buggy','productPrice' => 183.99,'productQty' => 5)
);
foreach($array as $value){
$query = "insert into (columnname,columnname) values (".$value['rmsId'].",'".$value['departmentName']."')";
//fire query
}
假设你的数组格式如图所示 您的阵列结构不正确,请尝试如图所示