Macca424
Macca424

Reputation: 65

Adding dynamic array to the end of a static array PHP

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

Upvotes: 1

Views: 658

Answers (2)

Barmar
Barmar

Reputation: 781210

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

Upvotes: 3

MER
MER

Reputation: 328

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

Upvotes: 2

Related Questions