当变量为null时,是否有一个循环会在php中回送到网页时排除该变量?

当变量为null时,是否有一个循环会在php中回送到网页时排除该变量?,php,arrays,loops,Php,Arrays,Loops,我从mysql数据库中提取信息作为关联数组。如果没有信息(如电话号码或第二条地址线),某些变量将为空。我提取信息并将其回送到网页上。但是,保留返回空值的位置为空。我想删除空白的那些记录的空白。< /P> 我试图创建一个foreach语句来循环数组,但它在一行中重复记录14次,一次不包括一个变量 示例: 数据库有10列: Class::members() 运行PDO后,我开始while循环并创建数组$member: 我用html和css样式将此信息回传到网页上 我想编写一些php代码,在空白时跳过

我从mysql数据库中提取信息作为关联数组。如果没有信息(如电话号码或第二条地址线),某些变量将为空。我提取信息并将其回送到网页上。但是,保留返回空值的位置为空。我想删除空白的那些记录的空白。< /P> 我试图创建一个foreach语句来循环数组,但它在一行中重复记录14次,一次不包括一个变量

示例:

数据库有10列:

Class::members()

运行PDO后,我开始while循环并创建数组$member:

我用html和css样式将此信息回传到网页上

我想编写一些php代码,在空白时跳过上面的空白,但在不空白时包含它。

使其看起来像:

我尝试创建一个方法来循环结果并跳过空变量,但没有成功。

这将返回每个记录的14个相同结果,并且所有结果都有一个间隙。我还尝试使用数组过滤器($members)。


有什么建议吗?

只需尝试
array\u filter()
就可以像这样从
$member
数组中删除Null-

$member = array_filter($member);

如果存在,它将从数组值中删除布尔值
false
、null、
'
。如果要限制该值,可以使用任何回调函数。

只需在不需要时不输出

$output = $member[1] . $member[2];
$output.= ($output!='' ? '<br/>' : '') . $member[3];
$output.= ($output!='' ? '<br/>' : '') . $member[4];
$output.= ($output!='' ? '<br/>' : '') . $member[5];
$output.= ($output!='' AND ($member[6]!='' OR $member[7]!='') ? ', ' : '') . $member[6] . $member[7];
$output.= ($output!='' ? '<br/>' : '') . $member[8];
$output.= ($output!='' ? '<br/>' : '') . $member[9];
echo $output;
$output=$member[1]$成员[2];
$output.=($output!=''?'
':'')$成员[3]; $output.=($output!=''?'
':'')$成员[4]; $output.=($output!=''?'
':'')$成员[5]; $output.=($output!=''和($member[6]!=''或$member[7]!=''),:''$成员[6]$成员[7]; $output.=($output!=''?'
':'')$成员[8]; $output.=($output!=''?'
':'')$成员[9]; echo$输出;

更新(2019年4月11日) 我认为这是一个更好的解决方案:

$output = $member[1] . $member[2];
$output.= ($output!='' ? "\n" : '') . $member[3];
$output.= ($output!='' ? "\n" : '') . $member[4];
$output.= ($output!='' ? "\n" : '') . $member[5];
$output.= ($output!='' AND ($member[6]!='' OR $member[7]!='') ? ', ' : '') . $member[6] . $member[7];
$output.= ($output!='' ? "\n" : '') . $member[8];
$output.= ($output!='' ? "\n" : '') . $member[9];

// we replace multiple NewLines with a single NewLine
$output = preg_replace('/\n+/', "\n", $output);
// we trim our string to remove the NewLines on left and write, if any
$output = trim($output);
// we then simply replace all NewLines with <BR>
echo nl2br($output);
$output=$member[1]$成员[2];
$output.=($output!=''?\n:'')$成员[3];
$output.=($output!=''?\n:'')$成员[4];
$output.=($output!=''?\n:'')$成员[5];
$output.=($output!=''和($member[6]!=''或$member[7]!=''),:''$成员[6]$成员[7];
$output.=($output!=''?\n:'')$成员[8];
$output.=($output!=''?\n:'')$成员[9];
//我们用一个换行符替换多个换行符
$output=preg_replace(“/\n+/”、“\n”、$output);
//我们修剪字符串以删除左边的换行符并写入(如果有的话)
$output=trim($output);
//然后,我们只需将所有换行符替换为
echo nl2br(输出);
它不会消除间隙。它返回了相同的信息,并包含了一个错误“Undefined offset:10”(10是间隙所在的位置)。我喜欢这种格式,但是,我仍然有相同的问题。它仍然会回显BR标记和间隙。还有其他想法吗?另外,为了防止$output重复输出两次,我设置了$output=''。导致上述代码显示不必要的br的
$member
数组中的值是什么?
$output.=($output!=''?
:'')$成员[8]
是一个可能存在或不存在的电话号码。当我执行
echo var_dump()
操作时,我会为丢失的电话号码获取
string(0)”
,当电话号码存在时获取
string(11)“555555”
    <?php $abs = array(Class::members()); foreach($mbs as $mb) { echo $mb;}?>
    John Smith
    222 One Drive
                   (GAP BECAUSE NULL)
    Somewhere, USA 00000
                   (GAP BECAUSE NULL)
    555555555
    John Smith
    222 One Drive
    Somewhere, USA 00000
    555555555
    foreach($member as $key => $value)

    /* variants I have tried in if() statment ($value != NULL), 
    ($value === NULL),($value == 'NULL'), ($value == ""), 
    (empty($value)), ($member[$value] == NULL)*/

    if($value == NULL) {
    continue; }
    }
$member = array_filter($member);
$output = $member[1] . $member[2];
$output.= ($output!='' ? '<br/>' : '') . $member[3];
$output.= ($output!='' ? '<br/>' : '') . $member[4];
$output.= ($output!='' ? '<br/>' : '') . $member[5];
$output.= ($output!='' AND ($member[6]!='' OR $member[7]!='') ? ', ' : '') . $member[6] . $member[7];
$output.= ($output!='' ? '<br/>' : '') . $member[8];
$output.= ($output!='' ? '<br/>' : '') . $member[9];
echo $output;
$output = $member[1] . $member[2];
$output.= ($output!='' ? "\n" : '') . $member[3];
$output.= ($output!='' ? "\n" : '') . $member[4];
$output.= ($output!='' ? "\n" : '') . $member[5];
$output.= ($output!='' AND ($member[6]!='' OR $member[7]!='') ? ', ' : '') . $member[6] . $member[7];
$output.= ($output!='' ? "\n" : '') . $member[8];
$output.= ($output!='' ? "\n" : '') . $member[9];

// we replace multiple NewLines with a single NewLine
$output = preg_replace('/\n+/', "\n", $output);
// we trim our string to remove the NewLines on left and write, if any
$output = trim($output);
// we then simply replace all NewLines with <BR>
echo nl2br($output);