productid Qty SupplierId
M0001 - S0001
M0002 - S0002
M0001 - S0001
M0002 - S0002
Quantity is an input. manually given by user
$data = json_decode(file_get_contents("php://input"));
$Product=$data->Product;
$quantity=$data->quantity;
$suppliername=$data->suppliername;
sort($suppliername); //S0001,S0001,S0002,S0002
foreach($suppliername as $index => $value)
{
$ProductArr = $Product[$index];
$QtyhArr = $quantity[$index];
$SupplierArr = $suppliername[$index];
$qry_resArr = $conn->prepare("INSERT INTO Table(Supplier,Product,Qty) VALUES (?,?,?)");
$qry_resArr->execute(array($SupplierArr,$ProductArr,$QtyhArr));
}
如何在foreach循环之前基于排序数组过滤数组值。我需要根据排序的数组传递product,qty值。这该怎么做?。谢谢你
Expected result: I need to pass into query below format
S0001,M0001,300
S0001,M0001,70
S0002,M0002,150
S0002,M0002,100
答案 0 :(得分:1)
在排序时不应更改值的索引,因此您应使用asort而不是sort()
所以它会像
asort($suppliername);
foreach($suppliername as $index => $value){
$ProductArr = $Product[$index];
$QtyhArr = $quantity[$index];
$SupplierArr = $suppliername[$index];
}
它根据需要提供输出