我得到了下面的代码,它在一个sql循环中,用来确定我在电子表格上输出了多少行。基本上不粘贴整个内容,因为它是一个相当长的语句,顶部的SQL语句将返回60行,其中将包含我在原始$data1数组中输入的变量。
$stmt2= $mysqV1->prepare("SELECT DISTINCT master_recipe, recipe, matl_id, comp_length, comp_width, comp_tck from components where recipe > 0 and matl_id > 0 order BY CAST(recipe AS UNSIGNED) ASC" );
foreach ($result2 as $key2=>$value2)
{
$data1[]=(array("Master Recipe"=>$master_recipe,"Recipe"=>$recipe,"Recipe Name"=>$recipe_name,"Material"=>$material,"Length"=>$comp_length,"Width"=>$comp_width,"Thickness"=>$comp_tck));
}然后,我有一个进一步的嵌套循环(在原始$result2循环中),它将确定我向该数组添加了多少元素,因为值将随着记录的不同而变化。我曾尝试声明一个数组,然后使用数组推送和数组合并,但它们似乎都不能满足我的要求。
$temp7 = array($master_recipe);
$stmt7= $mysqV1->prepare("Select * from machine where master_recipe = ? order by route_header_id asc" );
$stmt7->execute($temp7);
$result7=$stmt7->fetchAll();
foreach ($result7 as $key7=>$value7)
{
$station_id = $value7['route_header_id'];
$time_taken = $value7['time_hrs'];
$a[] = (array("StationID"=>$time_taken));
array_push($data1,$a);
}所以我想要做的是将$a的内容添加到$data1的末尾,给我一个数组值,然后打印到我的电子表格中(打印部分已经对$data1数组起作用了),但是它没有向它添加$a。
最终结果,我希望在$data1中的值的结果如下所示
$data1[]=(array("Master Recipe"=>$master_recipe,"Recipe"=>$recipe,"Recipe Name"=>$recipe_name,"Material"=>$material,"Length"=>$comp_length,"Width"=>$comp_width,"Thickness"=>$comp_tck,"$station_id1"=>$time_taken,"$station_id2"=>$time_taken2,"$station_id3"=>$time_taken3));发布于 2019-07-17 03:47:07
将要添加到$data1的行放入$a变量中,然后在将其推入$data1之前向该行添加新元素。
foreach ($result2 as $value2) {
$master_recipe = $value2['master_recipe'];
$recipe = $value2['recipe'];
...
$a = array("Master Recipe"=>$master_recipe,"Recipe"=>$recipe,"Recipe Name"=>$recipe_name,"Material"=>$material,"Length"=>$comp_length,"Width"=>$comp_width,"Thickness"=>$comp_tck);
$temp7 = array($master_recipe);
$stmt7= $mysqV1->prepare("Select route_header_id, time_hrs from machine where master_recipe = ? order by route_header_id asc" );
$stmt7->execute($temp7);
while ($value7 = $stmt7->fetch())
{
$station_id = $value7['route_header_id'];
$time_taken = $value7['time_hrs'];
$a[$station_id] = $time_taken;
}
$data1[] = $a;
}发布于 2019-07-17 03:48:41
如果您将初始的$data1集更改为以下内容:
$data1= array(
"Master Recipe"=>$master_recipe,
"Recipe"=>$recipe,
"Recipe Nme"=>$recipe_name,
"Material"=>$material,
"Length"=>$comp_length,
"Width"=>$comp_width,
"Thickness"=>$comp_tck
);然后,在你的循环中..
foreach ($result7 as $key7=>$value7)
{
$station_id = $value7['route_header_id'];
$time_taken = $value7['time_hrs'];
$data1[$station_id] = $time_taken;
}https://stackoverflow.com/questions/57063691
复制相似问题