2015-11-06 24 views
0

我想填充一个空值的数组中的字符串'' 我需要使用isset还是空来检查它吗?如何填写数组中的字符串?

这里是我的代码

echo table(); 

function table() { 
    $a = array ('0' => array('Jan de Boer', '213','440'), 
       '1' => array('Gerda Severin','214','442'), 
       '2' => array('Jean Dubois','215',''), 
       '3' => array('Peter Geringh','221','449'), 
       '4' => array('ricardo','666','666')); 

    echo "<table border='6px'> 
    <tr><th colspan='3'>Alle werknemers</th></tr> 
    <tr><th>Naam</th> 
    <th>kamer</th> 
    <th >Toestelnummer</th></tr>"; 

    for ($x=0;$x<5;$x++){ 
     echo "<tr>"; 
     for($y=0;$y<3;$y++){ 
      echo "<td>",$a[$x][$y].'</td>'; 
     } 
     echo "</tr>"; 
    } 
    echo "</table>"; 
} 

它需要填空''像绳子未知。

+0

是的,你需要。 –

+0

你用什么替换空字符串? – Chizzle

回答

1

如果字符串为空进行检查,并用值替换,在我的例子“未知”

echo table(); 
function table() 

{  
$a = array ('0' => array('Jan de Boer', '213','440'), 
      '1' => array('Gerda Severin','214','442'), 
      '2' => array('Jean Dubois','215',''), 
      '3' => array('Peter Geringh','221','449'), 
      '4' => array('ricardo','666','666')); 

echo "<table border='6px'> 
<tr><th colspan='3'>Alle werknemers</th></tr> 
<tr><th>Naam</th> 
<th>kamer</th> 
<th >Toestelnummer</th></tr>"; 

    for ($x=0;$x<5;$x++){ 
    echo "<tr>"; 
    for($y=0;$y<3;$y++){ 
    if($a[$x][$y] == "") $a[$x][$y] = 'UNKNOWN'; 
     echo "<td>",$a[$x][$y].'</td>'; 
    } 
    echo "</tr>"; 
} 

echo "</table>"; 

} 
+0

非常感谢你^^ –

+0

欢迎你@RicardoBuijsman –

0

这取决于你正在检查。 empty将返回true,如果字符串已设置且没有字符,而!isset将只返回true,如果该字符串根本不存在。

如果你只是想避免错误,你可以用''字符串使用!isset。如果你想确保字符串中有任何东西,请使用empty