我有一个来自mysql的数组,我想通过变量进行回显。我希望原始数组出于个人原因检查是否有任何空或空字符串被设置为字符串“ <null>
”。但我似乎无法获得预期的结果。预先感谢。
while ($row = mysql_fetch_array($RS2)) {
foreach ($row as $key => $value) {
if (empty($value)) {
$row[$key] = "<null>";
}
}
echo "serno=$row[serno];";
echo "date=$row[date];";
echo "time=$row[time];";
echo "nett=$row[nett];";
echo "amount=$row[amt];";
echo "\n";
}
当前回显时的当前结果如下:
serno=1003;date=2018-07-14;time=01:18:57;nett=;amount=500.00;
预期结果是:
serno=1003;date=2018-07-14;time=01:18:57;nett=<null>;amount=500.00;
答案 0 :(得分:2)
用户foreach循环如下:
foreach ($row as $key => $value) {
$row[$key] = empty($value) ? "" : $value;
}
它将空值替换为""
。
答案 1 :(得分:1)
//I forgot to put else
$otherArray = array();
while ($row = mysql_fetch_array($RS2)) {
foreach ($row as $key => $value) {
if (empty($value)) {
$otherArray[$key] = "<null>";
}else{//else here
otherArray[$key] = $value;
}
}
echo "serno=$otherArray[serno];";
echo "date=$otherArray[date];";
echo "time=$otherArray[time];";
echo "nett=$otherArray[nett];";
echo "amount=$otherArray[amt];";
echo "\n";
}
答案 2 :(得分:0)
您可以使用内置函数is_null
while ($row = mysql_fetch_array($RS2)) {
foreach ($row as $key => $value) {
if (is_null($value)) {
$var="<null>";
$row[$key] = htmlspecialchars($var);
}
}
echo "serno=$row[serno];";
echo "date=$row[date];";
echo "time=$row[time];";
echo "nett=$row[nett];";
echo "amount=$row[amt];";
echo "\n";
}
答案 3 :(得分:0)
//may be like that
$otherArray = array();
while ($row = mysql_fetch_array($RS2)) {
foreach ($row as $key => $value) {
if (empty($value)) {
$otherArray[$key] = "<null>";
}
otherArray[$key] = $value;
}
echo "serno=$otherArray[serno];";
echo "date=$otherArray[date];";
echo "time=$otherArray[time];";
echo "nett=$otherArray[nett];";
echo "amount=$otherArray[amt];";
echo "\n";
}
答案 4 :(得分:0)
foreach($row as $key => $value){
$row[$key] = !isset($value) ? NULL : $value; }
使用isset以便轻松检查NULL值