[英]php arrays: how to print only array values but not keys
此代码循环遍历mysql表并打印出空/空字段。 然而,它打印数组值和这样的键
Array (
[0] => Field "dob" on entry "1" is empty/null
[1] => Field "user_name" on entry "7" is empty/null
)
如何field "dob" on entry "1" is empty/null
打印此field "dob" on entry "1" is empty/null
$sql = "SELECT * FROM userinfo";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res)) {
foreach($row as $key => $field) {
if(empty($field)) {
$emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $key, $row['userid']);
}
}
}
print_r($emptyFields);
echo implode('<br>', $emptyFields);
那是因为您使用print_r输出该数组。 因此输出的格式对人类可读。 为了使它更漂亮,尝试像之前使用该字段一样迭代它:
foreach($emptyFields as $key => $field) {
echo('Field "'.$field.'" on entry "'.$emptyField['userid'].'" is empty/null');
}
$sql = "SELECT * FROM userinfo";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res)) {
foreach($row as $key => $field) {
if(empty($field)) {
$emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $key, $row['userid']);
}
}
}
我不知道我是否正确,但我认为你的问题的解决方案是:
将最后一行更改为:
$emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $field, $row['userid']);}}}print_r($emptyFields);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.