[英]PHP - Insert to Database from Multidimensional Array
I have a question regarding this problem (as per title). 我对这个问题有疑问(根据标题)。 This is my array
这是我的阵列
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[1] => Array
(
[0] => 3
[1] => 2
)
[2] => Array
(
[0] => 1
[1] => 0
)
)
for more detail, you can see the picture 有关详细信息,您可以看到图片
The first dimensional will always have the same length (in this case is 3). 第一维将始终具有相同的长度(在这种情况下为3)。 What I want to achieve is : I want to fetch the data from this multidimensional array so I can use the value to update my database.
我想要实现的是:我想从这个多维数组中获取数据,以便我可以使用该值来更新我的数据库。 The syntax would be like this (I will using loop):
语法就像这样(我将使用循环):
$sql = "UPDATE table_name SET column1='".$body[$i]."', column2='".$grip[$i]."', column3='".$gear[$i]."' WHERE `variant`='".$variant[$i]."'";
Is it possible? 可能吗? In this case, the
variant
column will be A,B. 在这种情况下,
variant
列将是A,B。
I already saw these question 我已经看到了这些问题
but I still don't understand. 但我还是不明白。
This is my code 这是我的代码
if ($stmt2 = $conn->prepare("SELECT * FROM ms WHERE variant in (SELECT variant FROM pwhorder WHERE times = '$time' AND NOT quantity = 0)")) {
$stmt2->execute();
$result2 = $stmt2->get_result();
$num_of_rows2 = $result2->num_rows;
while ($row2 = $result2->fetch_assoc()) {
$amountb1[]=$row2["amount1"];
$amountb2[]=$row2["amount2"];
$amountb3[]=$row2["amount3"];
}
$stmt2->free_result();
$stmt2->close();
}
$stock=array($amountb1,$amountb2,$amountb3);
I do this because at first I want to compare $stock
with $request
. 我这样做是因为起初我想比较
$stock
和$request
。 The code for $request
is similar with $stock
, it is just taken from different table. $request
的代码与$stock
类似,它只是从不同的表中获取。
This seems like a perfect case in which associative arrays would solve your problem nicely. 这似乎是一个完美的案例,其中关联数组可以很好地解决您的问题。 However since you stated the problem is utilizing indexed arrays, you would need to step through each array and perform actions, however you'll have to maintain a companion array of values for the interpretation of the indexes to their keys.
但是,由于您声明问题是使用索引数组,因此您需要逐步执行每个数组并执行操作,但是您必须维护一个值的伴随数组,以便将索引解释为其键。
Start with your lookup arrays to be a companion to your data. 从查找数组开始,作为数据的伴侣。
$top_level = array('Body', 'Grip', 'Gear');
$second_level = array('A','B');
Then you will want to process your data array and store into it's associated companioned keyed array. 然后,您将需要处理数据数组并将其存储到其关联的伴随键控数组中。
So let's say your multidimensional array would be name $data
所以假设您的多维数组是名称
$data
for( $d = 0 ; $d < count($data); $d++ )
{
for( $s = 0; $s < count($data[$d]); $s++ )
{
$output[$top_level[$d]][$second_level[$s]] = $data[$d][$s];
}//END FOR S < DATA{D}
}//END FOR D < COUNT(DATA)
Now this is currently untested code (I'm not within reach of a PHP compiler for testing). 现在这是当前未经测试的代码(我无法通过PHP编译器进行测试)。 If you do get any errors drop a comment and I'd be happy to update.
如果您收到任何错误,请发表评论,我很乐意更新。
The resulting $output
array should be the formatted data you are looking for if I am understanding you correctly. 如果我理解正确的话,生成的
$output
数组应该是您正在寻找的格式化数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.