如何在数组中填充字符串?

How to fill in a string in an array?

我正在尝试在数组值中填充一个字符串,该数组值为空 '' 我需要先用isset还是empty来检查?

这是我的代码

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

需要填空''如未知字符串

执行检查字符串是否为空并替换为值,在我的示例中 'UNKNOWN'

    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 将 return 为真,而如果字符串根本不存在,!isset 将仅 return 为真。

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