如何在数组中填充字符串


How to fill in a string in an array?

我试图在一个''为空的数组值中填充一个字符串我需要先使用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>";
}

它需要像一个未知的字符串一样填写空白的''

检查字符串是否为空并替换为值,在我的示例中为"未知"

    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>";
}

这取决于您要检查的内容。如果字符串已设置并且其中没有字符,则empty将返回true,而!isset仅在字符串根本不存在的情况下返回true。

如果您只是想避免错误,并且可以使用''字符串,请使用!isset。如果您希望确保字符串中确实包含任何内容,请使用empty

相关文章: