Creating an array from if then statements but having problems displaying only the results that exist

I have an array created using if statements:

if($BaseIngCode == "1"){$total1 = $ing1 + $total1; $data1 = array(); $data1 = array('ID'=>$BaseIngCode,'Total'=>$total1, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total1 - ING $BaseIngCode</td>";
}elseif($BaseIngCode == "2"){$total2 = $ing1 + $total2; $data2 = array(); $data2 = array('ID'=>$BaseIngCode,'Total'=>$total2, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total2 - ING $BaseIngCode</td>";

etc…
Outside of the While loop the array is being called as follows:

$result = array_merge_recursive($data1, $data2, $data3, $data4, $data6, $data96, $data28, $data33, $data31, $data35, $data93);
var_dump($result);

but if there is no data for $data3 then i get the following error:
Warning: array_merge_recursive(): Argument #3 is not an array in /home/siteaddress.co.uk/specsite/ingredients/opt4new3.php on line 313
NULL

There is undoubtably a better way of doing this but I am literally going round in loops and circles. How can I just call existing information from the array?

It was not easy to follow your script :frowning:

Far better to enclose the script between three backticks each on their own line both.
Before and after the script, or use the </> to enclose the script.

I have also added some script which may may it easier to $DEBUG:

<?php 
declare(strict_types=1);
ini_set('display_errors', 'true');
error_reporting(-1);

# DEBUG because I do not know the correct values.
  if($DEBUG = TRUE):
    $BaseIngCode = $BaseIngCode ?? 0;

    $data1       = $data2  = $data3  = $data4  = [1];
    $data6       = $data96 = $data28 = $data33 = [4];
    $data31      = $data35 = $data93 = [7];
    $ing1        = $ing1        ?? 0;
    $total1      = $total1      ?? 0;
    $total2      = $total2      ?? 0; 
    $COO         = $coo         ?? 'COO';
    $IngredientName2 = $IngredientName2 ?? '$IngredientName2'; 
  endif;  

echo '<hr>';
  echo 'START: if/elseif/else';
echo '<hr>';
if($BaseIngCode === "1")
{
  echo '<br> line ==> ' .__line__;
  echo '<br><b> {if} $BaseIngCode  </b> == ' .$BaseIngCode;

  $total1 = $ing1 + $total1; 
//$data1  = array(); 
  $data1  = array
  (
    $result = array_merge_recursive
    (
      $data1, 
      $data2, 
      $data3, 
      $data4, 
      $data6, 
      $data96, 
      $data28, 
      $data33, 
      $data31, 
      $data35, 
      $data93
    )
  );  
  echo '<br> line ==> ' .__line__;
  echo '<br> var_dump($result) ==> ' .var_dump($result);
  /*
    'ID'    =>  $BaseIngCode,
    'Total' =>  $total1, 
    'Name'  =>  $IngredientName2, 
    'COO'   =>  $COO
  );
  */
  echo '<table><tr>';
    echo "<td align=center> Total $total1 - ING $BaseIngCode</td>";
  echo '</tr></table>';  

}elseif($BaseIngCode == "2"){
  echo '<br> line ==> ' .__line__;
  echo '<br><b> {elseif} $BaseIngCode  </b> == ' .$BaseIngCode;

  $total2 = $ing1 + $total2; 
//$data2  = array(); 
  $data2  = array
  (
    'ID'    =>  $BaseIngCode,
    'Total' =>  $total2, 
    'Name'  =>  $IngredientName2, 
    'COO'   =>  $COO
  );
  echo '<br> line ==> ' .__line__;
  echo "<td align=center> Total $total2 - ING $BaseIngCode</td>";

}else{
  echo '<br> line ==> ' .__line__;
  echo '<br><b> {else} $BaseIngCode  </b> == ' .$BaseIngCode;
}//  

echo '<hr>';
echo 'FINISH: if/elseif/else';
echo '<hr>';

# Outside of loop
  $arrays = 
  [
    $data1, 
    $data2, 
    $data3, 
    $data4, 
    $data6, 
    $data96, 
    $data28, 
    $data33, 
    $data31, 
    $data35, 
    $data93
  ];
  echo '<br> line ==> ' .__line__;
  foreach($arrays as $id => $array):  
    echo '<br>gettype($array) ' .$id .' ==> ' .gettype($array);
  endforeach;
    
$result = array_merge_recursive
  (
    $data1, 
    $data2, 
    $data3, 
    $data4, 
    $data6, 
    $data96, 
    $data28, 
    $data33, 
    $data31, 
    $data35, 
    $data93
  );
  echo '<br> line ==> ' .__line__;
  echo '<br><br> gettype($result) ==> ' .gettype($result);
  echo '<br>';
  echo '<br><b> var_dump($result) </b> ==> '; var_dump($result);
  echo '<br> line ==> ' .__line__;
  foreach($result as $id => $array):  
    echo '<br>gettype($array) ' .$id .' ==> ' .gettype($array);
  endforeach;

It may help to have more of an overview of what you’re trying to achieve - the code you have does seem overly complicated but it’s difficult to suggest improvements without knowing what you have, and what you need out of it.

The error you get is simply because you’re trying to use an array that does not exist. Perhaps your code would be better to loop through and build the $result array directly.

<?php
require_once("../menu.php");
?>
<html>
	<head>
		<title>BakeArt Admin Products List</title>
		<style type="text/css"><!--body,td,th {	font-family: Verdana, Arial, Helvetica, sans-serif;	font-size: 12;}--></style>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	</head>
	<body>
		<div align="center"><?$username="username";$password="password";$database="database";
		mysql_connect("localhost",$username,$password);
		@mysql_select_db($database) or die( "Unable to select database");

		$MasterBACode=$_GET['MasterBACode'];
		$aitem_name=$_GET['item_name'];
		$medium=$_GET['medium'];

		echo "<div align=center>
			<div align=center>
				<table width=518 >
					<tr>
						<td>Master Product Builder</td>
					</tr>
					<tr>
						<td><form action=opt4new4.php method=post  validateThisFrom (this);'>
							<input type=hidden name=item_name value='$item_name'>
							<input type=hidden name=medium value='$medium'>
							<input type=hidden name=SupplierName value='$SupplierName'>
							<input type=hidden name=MasterBACode value='$MasterBACode'>
							<div align=center>

								<table width=100% border=1 cellspacing=1 cellpadding=1>
								<tr>
									<td width=40%><div align=right><font size=1>5. BA Code:</td>
									<td><font size=1>BA$MasterBACode</td>
								</tr>
								<tr>
									<td><div align=right><font size=1>Item Name: </div></td>
									<td><font size=1>$aitem_name</td>
								</tr>
								<tr>
									<td><div align=right><font size=1>Medium: </div></td>
									<td><font size=1>$medium</td>
								</tr>
								</table><br><br>
							";
				
									$query="SELECT * FROM ProductBuilder
									INNER JOIN ProductCodes ON ProductBuilder.SubBACode = ProductCodes.id
									WHERE MasterBACode='$MasterBACode' ORDER BY SubPercentage DESC" ;
									$result=mysql_query($query);	
									$rowcount=mysql_num_rows($result); 
			            if(mysql_num_rows($result) > 0){
										
										echo "
								<table width=700 border=1>
								<tr>
									<td colspan=4><b>Current Ingredients</td>
								</tr>
								<tr>
									<td width=130><b>Code</td>
									<td ><b>Ingredient Name</td>
									<td width=70 align=center><b>%</td>
									<td width=30>&nbsp;</td>
								</tr>";
										$sum=0;

										while($row = mysql_fetch_array($result)) 
            			{
										$SubPercentage = $row['SubPercentage'];
										$Percentage = $row['SubPercentage'];
										$Baseid = $row['Baseid'];
										$COO = $row['COO'];
										$Subitem_name = $row['item_name'];
									  $SubBACode = $row['SubBACode'];
										$Masterid = $row['Masterid'];
											
									  echo "
										<tr>
												<td>BA$SubBACode</td>
												<td>$Subitem_name</td>
												<td align=center>$SubPercentage</td>
												<td><a href='opt4delete1.php?Masterid=$Masterid&MasterBACode=$MasterBACode'>Delete</a></td>
										</tr>";
											$value = $row['SubPercentage'];
											$sum += $value;
										}
										
										echo "<tr><td>&nbsp;</td><td align=right>Percentage Total</td>";
												if ($sum <= 100){
											echo"<td align=center>$sum%</td>";
										}elseif ($sum > 100){
										echo"<td align=center><font color=red>$sum%</td>";
										}
										
													echo"	<td colspan=2>&nbsp;</td>
													</tr>
												<tr><td colspan=4 align=center>To add another ingredient complete the form below</td></tr>
											</table><br><br>";
										}
								
								 
								else { echo "<b>There are currently no ingredients assigned to this product.<br> To add an ingredient please complete the form below.<br>&nbsp;";
										 }
								 
							$query="SELECT * FROM ProductCodes WHERE Master = 'Sub' ORDER By item_name" ;
							$result=mysql_query($query);
							$num=mysql_numrows($result);	
							echo "
								<table><tr>
									<td>Select an ingredient</td>
									<td><b><select name='SubBACode' id='SubBACode'>"
									;$c=0;while ($c < $num) {$index=mysql_result($result,$c,"item_name");
									$item_name=mysql_result($result,$c,"item_name");
									$id=mysql_result($result,$c,"id");
									echo "<option value='$id'>BA$id - $item_name</option>"
									;$c++;}?>
									<?echo "</select></b></td>
								</tr>
								<tr>
									<td>Percentage:<font size=1> Use numbers only (e.g 0.5 not <1)</td>
									<td><input type=text name='Percentage'></td>
								</tr>
								<tr>
									<td>Recipe Volume:</td>
									<td><input type=text name='RecipeVolume'></td>
								</tr>
								<tr>
									<td>&nbsp;</td>
									<td><input type=Submit id='add item' value='add item'></td>
								</tr>
							</table><br><br>
																	<div class='container'>
 
  <a href='#demo' class='btn btn-info' data-toggle='collapse'>Click For Ingredients Breakdown</a>
  <div id='demo' class='collapse'>
							";
			
								$query="SELECT * FROM ProductBuilder
									INNER JOIN ProductCodes ON ProductBuilder.SubBACode = ProductCodes.id
									
									WHERE MasterBACode='$MasterBACode' ORDER BY SubPercentage DESC" ;
									$result=mysql_query($query);	
									$rowcount=mysql_num_rows($result); 
			            if(mysql_num_rows($result) > 0){
										



										$sum=0;

										while($row = mysql_fetch_array($result)) 
            			{
										$SubPercentage = $row['SubPercentage'];
										$Baseid = $row['Baseid'];
										$COO = $row['COO'];
										$Subitem_name = $row['item_name'];
									  $SubBACode = $row['SubBACode'];
										$BigPercentage = $row['SubPercentage'];

									  echo "

									<table id=1 border=0>
										<tr>
											<td><b>BA$SubBACode - $Subitem_name</td>
											<td align=right><b>Percentage of total - $BigPercentage%</td>
										</tr>
										<tr>
											<td colspan=4>";
																					$query3="SELECT ProductBaseList.Baseid, ProductBaseList.BACode, ProductBaseList.BaseIngCode, ProductBaseList.Percentage, ProductBaseList.COO, BaseIngredients.id, BaseIngredients.IngredientType, BaseIngredients.IngredientName, BaseIngredients.DisplayName, BaseIngredients.Display  FROM ProductBaseList 
																						INNER JOIN BaseIngredients ON ProductBaseList.BaseIngCode = BaseIngredients.id
																						WHERE BACode=$SubBACode " ;
																						$result3=mysql_query($query3);
																						if(mysql_num_rows($result3) > 0){
																							echo "
																				<table id=2 width=600 border=1>
																					<tr>
																						<td width=300 bgcolor=#D3D3D3><font size=1>Ingredient Name</td>
																						<td width=70 bgcolor=#D3D3D3 align=center><font size=1>%</td>
																						<td width=300 bgcolor=#D3D3D3><font size=1>Country Of Origin</td>
																						<td bgcolor=#D3D3D3><font size=1>Percentage of total</td>
																					</tr>";
																									$sum=0;
																							//$data = array();
		
																										while($row3 = mysql_fetch_array($result3)) 
																										{
																											
																											$SubPercentage = $row3['Percentage'];
																											$IngredientName2 = $row3['IngredientName'];
																											$BaseIngCode = $row3['BaseIngCode'];
																											$Baseid = $row3['Baseid'];
																											$COO = $row3['COO'];
																											$ing1=0;
																											$ing1 = $BigPercentage / 100 * $SubPercentage;


																						echo "
																						
																					<tr>
																						<td><font size=1>$SubBACode $BaseIngCode $IngredientName2</td>
																						<td align=center><font size=1>$SubPercentage</td>
																						<td><font size=1>$COO</td>
																						<td><font size=1>$ing1%</td>
																				";
 																							     
																										
								 if($BaseIngCode == "1"){$total1 = $ing1 + $total1; $data1 = array(); $data1 = array('ID'=>$BaseIngCode,'Total'=>$total1, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total1 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "2"){$total2 = $ing1 + $total2; $data2 = array(); $data2 = array('ID'=>$BaseIngCode,'Total'=>$total2, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total2 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "3"){$total3 = $ing1 + $total3; $data3 = array(); $data3 = array('ID'=>$BaseIngCode,'Total'=>$total3, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total3 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "4"){$total4 = $ing1 + $total4; $data4 = array(); $data4 = array('ID'=>$BaseIngCode,'Total'=>$total4, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total4 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "5"){$total5 = $ing1 + $total5; $data5 = array(); $data5 = array('ID'=>$BaseIngCode,'Total'=>$total5, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total5 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "6"){$total6 = $ing1 + $total6; $data6 = array(); $data6 = array('ID'=>$BaseIngCode,'Total'=>$total6, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total6 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "7"){$total7 = $ing1 + $total7; $data7 = array(); $data7 = array('ID'=>$BaseIngCode,'Total'=>$total7, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total7 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "8"){$total8 = $ing1 + $total8; $data8 = array(); $data8 = array('ID'=>$BaseIngCode,'Total'=>$total8, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total8 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "9"){$total9 = $ing1 + $total9; $data9 = array(); $data9 = array('ID'=>$BaseIngCode,'Total'=>$total9, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total9 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "10"){$total10 = $ing1 + $total10; $data10 = array(); $data10 = array('ID'=>$BaseIngCode,'Total'=>$total10, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total10 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "11"){$total11 = $ing1 + $total11; $data11 = array(); $data11 = array('ID'=>$BaseIngCode,'Total'=>$total11, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total11 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "12"){$total12 = $ing1 + $total12; $data12 = array(); $data12 = array('ID'=>$BaseIngCode,'Total'=>$total12, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total12 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "14"){$total14 = $ing1 + $total14; $data14 = array(); $data14 = array('ID'=>$BaseIngCode,'Total'=>$total14, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total14 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "16"){$total16 = $ing1 + $total16; $data16 = array(); $data16 = array('ID'=>$BaseIngCode,'Total'=>$total16, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total16 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "18"){$total18 = $ing1 + $total18; $data18 = array(); $data18 = array('ID'=>$BaseIngCode,'Total'=>$total18, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total18 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "19"){$total19 = $ing1 + $total19; $data19 = array(); $data19 = array('ID'=>$BaseIngCode,'Total'=>$total19, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total19 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "20"){$total20 = $ing1 + $total20; $data20 = array(); $data20 = array('ID'=>$BaseIngCode,'Total'=>$total20, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total20 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "21"){$total21 = $ing1 + $total21; $data21 = array(); $data21 = array('ID'=>$BaseIngCode,'Total'=>$total21, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total21 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "22"){$total22 = $ing1 + $total22; $data22 = array(); $data22 = array('ID'=>$BaseIngCode,'Total'=>$total22, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total22 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "23"){$total23 = $ing1 + $total23; $data23 = array(); $data23 = array('ID'=>$BaseIngCode,'Total'=>$total23, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total23 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "24"){$total24 = $ing1 + $total24; $data24 = array(); $data24 = array('ID'=>$BaseIngCode,'Total'=>$total24, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total24 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "26"){$total26 = $ing1 + $total26; $data26 = array(); $data26 = array('ID'=>$BaseIngCode,'Total'=>$total26, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total26 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "28"){$total28 = $ing1 + $total28; $data28 = array(); $data28 = array('ID'=>$BaseIngCode,'Total'=>$total28, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total28 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "29"){$total29 = $ing1 + $total29; $data29 = array(); $data29 = array('ID'=>$BaseIngCode,'Total'=>$total29, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total29 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "30"){$total30 = $ing1 + $total30; $data30 = array(); $data30 = array('ID'=>$BaseIngCode,'Total'=>$total30, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total30 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "31"){$total31 = $ing1 + $total31; $data31 = array(); $data31 = array('ID'=>$BaseIngCode,'Total'=>$total31, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total31 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "33"){$total33 = $ing1 + $total33; $data33 = array(); $data33 = array('ID'=>$BaseIngCode,'Total'=>$total33, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total33 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "34"){$total34 = $ing1 + $total34; $data34 = array(); $data34 = array('ID'=>$BaseIngCode,'Total'=>$total34, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total34 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "35"){$total35 = $ing1 + $total35; $data35 = array(); $data35 = array('ID'=>$BaseIngCode,'Total'=>$total35, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total35 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "36"){$total36 = $ing1 + $total36; $data36 = array(); $data36 = array('ID'=>$BaseIngCode,'Total'=>$total36, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total36 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "37"){$total37 = $ing1 + $total37; $data37 = array(); $data37 = array('ID'=>$BaseIngCode,'Total'=>$total37, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total37 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "38"){$total38 = $ing1 + $total38; $data38 = array(); $data38 = array('ID'=>$BaseIngCode,'Total'=>$total38, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total38 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "39"){$total39 = $ing1 + $total39; $data39 = array(); $data39 = array('ID'=>$BaseIngCode,'Total'=>$total39, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total39 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "40"){$total40 = $ing1 + $total40; $data40 = array(); $data40 = array('ID'=>$BaseIngCode,'Total'=>$total40, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total40 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "41"){$total41 = $ing1 + $total41; $data41 = array(); $data41 = array('ID'=>$BaseIngCode,'Total'=>$total41, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total41 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "42"){$total42 = $ing1 + $total42; $data42 = array(); $data42 = array('ID'=>$BaseIngCode,'Total'=>$total42, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total42 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "44"){$total44 = $ing1 + $total44; $data44 = array(); $data44 = array('ID'=>$BaseIngCode,'Total'=>$total44, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total44 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "53"){$total53 = $ing1 + $total53; $data53 = array(); $data53 = array('ID'=>$BaseIngCode,'Total'=>$total53, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total53 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "56"){$total56 = $ing1 + $total56; $data56 = array(); $data56 = array('ID'=>$BaseIngCode,'Total'=>$total56, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total56 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "58"){$total58 = $ing1 + $total58; $data58 = array(); $data58 = array('ID'=>$BaseIngCode,'Total'=>$total58, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total58 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "68"){$total68 = $ing1 + $total68; $data68 = array(); $data68 = array('ID'=>$BaseIngCode,'Total'=>$total68, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total68 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "75"){$total75 = $ing1 + $total75; $data75 = array(); $data75 = array('ID'=>$BaseIngCode,'Total'=>$total75, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total75 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "80"){$total80 = $ing1 + $total80; $data80 = array(); $data80 = array('ID'=>$BaseIngCode,'Total'=>$total80, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total80 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "81"){$total81 = $ing1 + $total81; $data81 = array(); $data81 = array('ID'=>$BaseIngCode,'Total'=>$total81, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total81 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "87"){$total87 = $ing1 + $total87; $data87 = array(); $data87 = array('ID'=>$BaseIngCode,'Total'=>$total87, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total87 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "88"){$total88 = $ing1 + $total88; $data88 = array(); $data88 = array('ID'=>$BaseIngCode,'Total'=>$total88, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total88 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "90"){$total90 = $ing1 + $total90; $data90 = array(); $data90 = array('ID'=>$BaseIngCode,'Total'=>$total90, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total90 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "91"){$total91 = $ing1 + $total91; $data91 = array(); $data91 = array('ID'=>$BaseIngCode,'Total'=>$total91, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total91 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "92"){$total92 = $ing1 + $total92; $data92 = array(); $data92 = array('ID'=>$BaseIngCode,'Total'=>$total92, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total92 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "93"){$total93 = $ing1 + $total93; $data93 = array(); $data93 = array('ID'=>$BaseIngCode,'Total'=>$total93, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total93 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "94"){$total94 = $ing1 + $total94; $data94 = array(); $data94 = array('ID'=>$BaseIngCode,'Total'=>$total94, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total94 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "96"){$total96 = $ing1 + $total96; $data96 = array(); $data96 = array('ID'=>$BaseIngCode,'Total'=>$total96, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total96 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "97"){$total97 = $ing1 + $total97; $data97 = array(); $data97 = array('ID'=>$BaseIngCode,'Total'=>$total97, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total97 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "102"){$total102 = $ing1 + $total102; $data102 = array(); $data102 = array('ID'=>$BaseIngCode,'Total'=>$total102, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total102 - ING $BaseIngCode</td>";
						}elseif($BaseIngCode == "103"){$total103 = $ing1 + $total103; $data103 = array(); $data103 = array('ID'=>$BaseIngCode,'Total'=>$total103, 'Name'=>$IngredientName2, 'COO'=>$COO);echo "<td align=center> Total $total103 - ING $BaseIngCode</td>";

						}
																							
																											$A = $IngredientName2;
																											$B = $SubPercentage;
																											$C = $B + $SubPercentage;
																											$total = $total + $C;
																										}
																							
								echo "</td>
										</tr>
									</table></td></tr></table><br>
									";
 																		} else { echo "<b>There are currently no ingredients assigned to this product.<br> To add an ingredient please complete the form below.<br>&nbsp;";}
										

											}
										
								echo "
										</div>
									</div><table width=600>
													<tr>
														<td colspan=3> <br><br><b>Final Ingredients List</td>
													</tr>
												</table>
												<table width=600 border=1>
													<tr>
														<td bgcolor=#D3D3D3><b>Ingredient Name</td>
														<td bgcolor=#D3D3D3><b>%</td>
														<td bgcolor=#D3D3D3><b>Country Of Origin</td>
													</tr>
													<tr>
														<td> ";
										print_r($data);

										

										
										echo "1 Sub- $SubPercentage </td></tr></table>";
										 //echo "Ingredient: {$data['Name']} Total: {$data['Total']}%  Country OF Origin: {$data['COO']}";



										
										
										


//$result = array_merge_recursive($ar1, $ar2);										
$result = array_merge_recursive($data1, $data2, $data3, $data4, $data6, $data96, $data28, $data33, $data31, $data35, $data93);
var_dump($result);
									}
			
echo"<br><br>&nbsp;";			?>
</body></html>

This is the full code for the page. I was trying to show less to make it simpler but obviously got it wrong!
I’m adapting old code and haven’t really got the resources to re-write everything.
I am trying to just have a list at the bottom of the page of all of the ingredients in descending order by $total but with each ingredient only to appear once no matter how many times it appears in the results.
I then also need to just list the titles in descending order as they would be on an ingredients decleration label.
It’s been a long time since I did any kind of coding so I’ve probably forgotten more than I ever knew and I hope this makes some kind of sense!

Master Product Builder
5. BA Code:
BA13710
Item Name:
Medium:

Current Ingredients
Code Ingredient Name %
BA13720 Silk Sugarpaste MA 45 Delete
BA13718 Beetroot Powder 5 Delete
Percentage Total 50%
To add another ingredient complete the form below

Select an ingredient
Percentage: Use numbers only (e.g 0.5 not <1)
Recipe Volume:

BA13720 - Silk Sugarpaste MA Percentage of total - 45%
Ingredient Name % Country Of Origin Percentage of total
13720 1 Sugar 70 -TBC 31.5% Total 31.5
13720 4 Glucose Syrup 10 -TBC 4.5% Total 4.5
13720 2 Palm Oil 5 -TBC 2.25% Total 2.25
13720 35 Cornflour 15 Albania 6.75% Total 6.75

BA13718 - Beetroot Powder Percentage of total - 5%
Ingredient Name % Country Of Origin Percentage of total
13718 93 E162 Beetroot juice concentrate 98 -TBC 4.9% Total 4.9 - ING 93
13718 102 Maltodextrin 1 -TBC 0.05% Total 0.05 - ING 102
13718 103 E330: Citric Acid 1 -TBC 0.05% Total 0.05 - ING 103

Final Ingredients List
Ingredient Name % Country Of Origin
1 Sub- 1

Warning: array_merge_recursive(): Argument #3 is not an array in /home/bakeart/specsite.bakeart.co.uk/specsite/ingredients/opt4new3.php on line 313
NULL

Then a quick way to get around the specific error that you are having might be to create all the arrays prior to your loop, and remove the bit of code inside your conditionals that only creates them for specific values of $BaseIngCode. That way they will all exist, but some might be empty, and that might allow array_merge() to work as you want it to.

Stand by for comments about using the no-longer-supported mysql functions, though - they’re not part of the current language.

I’ve ended up with the following array:

Array
(
    [ID] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 
            [3] => 4
        )

    [Total] => Array
        (
            [0] => 66.5
            [1] => 4.75
            [2] => 0
            [3] => 9.5
        )

    [Name] => Array
        (
            [0] => Sugar
            [1] => Palm Oil
            [2] => 
            [3] => Glucose Syrup
        )

    [COO] => Array
        (
            [0] => -TBC
            [1] => -TBC
            [2] => 
            [3] => -TBC
        )

)

I’ve tried:

$sortresults = array();
foreach ($result as $key => $value) { 
    $sortresults[$key] = $value['Total'];
}
array_multisort($sortresults, SORT_ASC, $result);										
										
echo '<pre>', print_r($result, true), '</pre>';

Which gives me

`Array
(
    [COO] => 
    [ID] => 
    [Name] => 
    [Total] => 
)`

But this is just the key’s being sorted alphabetically.
I need to sort the results by descending total and not display the empty record [2]. I’ve never used array’s before incase you couldn’t tell! Is this possible? Sorry for all the questions, I’m trying to do a million things at once!

The $sortresults array is only “temporary” use for sorting, you will want to use $result array instead.

Amended but it still doesn’t sort in any order.

Array
(
    [COO] => Array
        (
            [0] => -TBC
            [1] => -TBC
            [2] => 
            [3] => -TBC
            [4] => Albania, Aruba, Bermuda
        )

    [ID] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 
            [3] => 4
            [4] => 5
        )

    [Total] => Array
        (
            [0] => 66.5
            [1] => 4.75
            [2] => 0
            [3] => 9.5
            [4] => 6.75
        )

    [Name] => Array
        (
            [0] => Sugar
            [1] => Palm Oil
            [2] => 
            [3] => Glucose Syrup
            [4] => Cornflour
        )

)
$result = array_merge_recursive($data1, $data2, $data3, $data4, $data5);

		$sortresults = array();
		foreach ($result as $key => $value) { 
				$sortresults[$key] = $value['Total'];
		}
		array_multisort($sortresults, SORT_DESC, $result);										

		echo '<pre>', print_r($result, true), '</pre>';

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.