I am trying to get the following variable into an array which basically contains a list of numbers separated by spaces. When I attempt to apply basic array formulas that should work it makes each individual number into a separate array. How would I get all the numbers into one array?
Partial Code:
echo "<h2>Table:</h2>";
echo "<table border='1'><tr>";
// printing table headers
for($i = 0; $i < $fields_num; $i++) {
$field = mysql_fetch_field($result);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>
";
// printing table rows
$rn = 0;
$projsum = 0;
while($row = mysql_fetch_row($result)) {
$rn = $rn + 1;
if($rn == 1) {
$presproj = $row[0];
$projrow = 1;
}
if($row[0] != $rowm1[0]) { // if present row col 1 differs from Previous
$col = 0;
echo "<tr>";
foreach($rowm1 as $cell) {
$col = $col + 1;
if($col == 1) {
echo "<td><small>Total</small></td>";
} elseif($col < count($rowm1)) {
echo "<td><small></small></td>";
} else {
echo "<td><small>";
echo $projsum;
echo "</small></td>";
$projsum = 0;
}
$projrow = 1;
}
echo "</tr>
";
}
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
$it = 0;
foreach($row as $cell) {
$it = $it + 1;
if(($it == 1) && ($projrow != 1)) {
echo "<td><small></small></td>";
} else {
echo "<td><small>" . $cell . "</small></td>";
}
}
echo "</tr>
";
$rowm1 = $row;
$projrow = $projrow + 1;
$projsum = $projsum + $row[count($row) - 1];
$sample .= End(end($row));
echo $sample;
}
mysql_free_result($result);
Variable:
$projsum=$projsum + $row[count($row)-1];
Using explode to make an array creates the following: $projsum_array = explode(" ", $projsum);
Array ( [0] => 62.92 ) Array ( [0] => 212.92 ) Array ( [0] => 238 ) Array ( [0] => 1 ) Array ( [0] => 151.58 ) Array ( [0] => 184.16 ) Array ( [0] => 713.99 ) Array ( [0] => 859.07 ) Array ( [0] => 864.32 ) Array ( [0] => 866.32 ) Array ( [0] => 897.57 ) Array ( [0] => 921.15 ) Array ( [0] => 924.15 ) Array ( [0] => 927.48 ) Array ( [0] => 944.15 ) Array ( [0] => 11 ) Array ( [0] => 50.83 )
If you want to split your string into an array you can use explode()
.
Check the documentation about this.
$projsum_array = explode(" ", $projsum);
Seems to me as what you are looking for is this:
$projsum .= End($row);
Notice the .=
means keep projsum and add.
And end() will get the last item in the array.
Edit: if it's the 50.83 from your array you are trying to get then it's one level lower in the array so you need to use
$projsum .= End(end($row));
It will throw an waening notice but you can suppress it with @ if you are sure the array does not change in structure.