【发布时间】:2017-08-01 19:51:53
【问题描述】:
我试图将以下变量放入一个数组,该数组基本上包含一个由空格分隔的数字列表。当我尝试应用应该工作的基本数组公式时,它会将每个单独的数字变成一个单独的数组。如何将所有数字放入一个数组中?
部分代码:
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>\n";
// 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>\n";
}
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>\n";
$rowm1 = $row;
$projrow = $projrow + 1;
$projsum = $projsum + $row[count($row) - 1];
$sample .= End(end($row));
echo $sample;
}
mysql_free_result($result);
变量:
$projsum=$projsum + $row[count($row)-1];
使用explode 创建一个数组会创建以下内容: $projsum_array = explode(" ", $projsum);
数组 ( [0] => 62.92 ) 数组 ( [0] => 212.92 ) 数组 ( [0] => 238 ) 数组 ( [0] => 1 ) 数组 ( [0] => 151.58 ) 数组 ( [0] => 184.16 ) 数组 ( [0] => 713.99 ) 数组 ( [0] => 859.07 ) 数组 ( [0] => 864.32 ) 数组 ( [0] => 866.32 ) 数组 ( [0] => 897.57 ) 数组 ( [0] => 921.15 ) 数组 ( [0] => 924.15 ) 数组 ( [0] => 927.48 ) 数组 ( [0] => 944.15 ) 数组 ( [0] => 11 ) 数组 ( [ 0] => 50.83)
【问题讨论】:
-
向我们展示变量的实际内容,而不是它的设置方式。您提供给我们的代码对于对您的项目一无所知的任何人(包括本网站上的所有人)都毫无意义
-
@GrumpyCrouton 这里是我尝试对变量使用explode 时的样子:它显示了单个数组中的所有数字:Array ([0] => 62.92) Array ([0] = > 212.92 ) 数组 ( [0] => 238 ) 数组 ( [0] => 1 ) 数组 ( [0] => 151.58 ) 数组 ( [0] => 184.16 ) 数组 ( [0] => 713.99 ) 数组 ( [0] => 859.07 ) 数组 ( [0] => 864.32 ) 数组 ( [0] => 866.32 ) 数组 ( [0] => 897.57 ) 数组 ( [0] => 921.15 ) 数组 ( [0] => 924.15 ) 数组 ( [0] => 927.48 ) 数组 ( [0] => 944.15 ) 数组 ( [0] => 11 ) 数组 ( [0] => 50.83 )
-
用一些格式将其粘贴到问题中。
-
你想得到 50.83 吗?
-
我们缺少太多信息。
$row是什么?它是如何格式化的?$projsum的初始条件是什么?我们需要 (a) 整个相关代码块,(b) 以及您的预期结果。