Php 如果数组包含值,则仅当下一个数组具有值时才起作用
我有下面的PHP脚本。当数组[5]有值且数组[4]为=“test”时,它可以正常工作,但当数组[5]没有值时,它就不工作了Php 如果数组包含值,则仅当下一个数组具有值时才起作用,php,arrays,Php,Arrays,我有下面的PHP脚本。当数组[5]有值且数组[4]为=“test”时,它可以正常工作,但当数组[5]没有值时,它就不工作了 // get file $file = "latest.log"; // test if file exists if(!file_exists($file)) { die("file not found"); } while(TRUE) { // open file $file = escapeshel
// get file
$file = "latest.log";
// test if file exists
if(!file_exists($file)) {
die("file not found");
}
while(TRUE) {
// open file
$file = escapeshellarg($file);
$file_handle = `tail -n 1 $file`;
# get line & convert to array
$array = explode(' ', $file_handle);
这很好,当我执行它时,我得到以下数组:
排列(
)
$player\u name=str\u replace(“,”,$player\u name);
$array[3]=$player\u name;
echo('Text:'.$array[4].'von.$player_name);
回声“;
打印(数组);
回声“//在顶部打印阵列
if($array[4]=“test”){
回声(“作品”);
}否则{
echo($array[4]);
//回显正确的值“test”!
}
}
我不明白这个错误。。。使用数组中的
,
也可以这样做。您的值肯定有换行符或空格。使用trim
删除它们。谢谢!现在它工作了。你的值肯定有换行符或空格。使用trim
删除它们。谢谢!现在它的工作。
[0] => [11:14:59]
[1] => [Server
[2] => thread/INFO]:
[3] => KaiAlexander
[4] => test
$player_name = str_replace("<", "", $array[3]);
$player_name = str_replace(">", "", $player_name);
$array[3] = $player_name;
echo('Text: ' . $array[4] ." von " . $player_name);
echo "<pre>";
print_r($array);
echo "</pre>"; //Prints the array on top
if ($array[4] == "test") {
echo("works");
} else {
echo($array[4]);
//Echo the correct value "test"!
}
}