1

I've got the following code which is within a sql loop to determine how many rows i output onto a spreadsheet. Basically without pasting the full thing as it's quite a lengthy statement, the top SQL statement will return 60 rows, which will contain the variables I enter into the original $data1 array.

$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));
  }

I then have a further nested loop (inside the original $result2 loop) which will determine how many elements i add to that array, as the value will change from record to record. I have tried to declare an array then use array push and array merge but neither of them seem to do what i want.

$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);
}

So what I would like this to do is add the contents of $a to the end of $data1 to give me 1 array value which then prints out to my spreadsheet(the print part is already working for the $data1 array) but it's not adding the $a to it.

Final result I would like to end up something like this for the value in $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));
13
  • You shouldn't be pushing $a every time through the loop, since each one contains the values that were pushed the previous time. Commented Jul 16, 2019 at 19:21
  • Why aren't you using $station_id? Did you mean to write array($station_id => $time_taken)? Commented Jul 16, 2019 at 19:22
  • 1
    Or maybe $a[$station_id] = $time_taken? Commented Jul 16, 2019 at 19:22
  • Can you show what you want the final result to look like? Commented Jul 16, 2019 at 19:22
  • @Barmar yes sorry i meant that. Commented Jul 16, 2019 at 19:28

2 Answers 2

3

Put the row that you're adding to $data1 in the $a variable, then you can add new elements to that row before you push it into $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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't look right. You are setting $row in the loop but then don't use it?
Oops, I switched from $row to $a and missed one of the edits.
Perfect, tested this and it does exactly what I need. Thanks.
2

What if you change your initial set of $data1 to this:

$data1= array(
    "Master Recipe"=>$master_recipe,
    "Recipe"=>$recipe,
    "Recipe Nme"=>$recipe_name,
    "Material"=>$material,
    "Length"=>$comp_length,
    "Width"=>$comp_width,
    "Thickness"=>$comp_tck
);

then, in your loop..

foreach ($result7 as $key7=>$value7)
{
    $station_id = $value7['route_header_id'];
    $time_taken = $value7['time_hrs'];
    $data1[$station_id] = $time_taken;
}

6 Comments

In chat he explained that $data1 is supposed to be a 2-dimensional array, not one row.
His "final result" looks like he wants $data to be an array of a single array?
That would be true if he wrote $data1 =, but $data1[] = means to push a new row into the 2-d array.
This is all inside another loop that's processing the result of another query from the master table.
Ok, got it. You're answer wins! :P
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.