php数组:如何只打印数组值而不打印键

时间:2011-03-23 10:00:12

标签: php arrays arraylist

此代码循环遍历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

的内容
$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);

4 个答案:

答案 0 :(得分:9)

echo implode('<br>', $emptyFields);

答案 1 :(得分:1)

那是因为您使用print_r输出该数组。因此输出的格式对人类可读。 为了使它更漂亮,尝试像之前使用该字段一样迭代它:

foreach($emptyFields as $key => $field) {
echo('Field "'.$field.'" on entry "'.$emptyField['userid'].'" is empty/null');
}

答案 2 :(得分:0)

$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']);
    }
  }
}

答案 3 :(得分:0)

我不知道我是否正确得到了这个,但我认为你的问题的解决方案是:

将最后一行更改为:

$emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $field, $row['userid']);}}}print_r($emptyFields);