尝试遍历列表以查找特定元素但无济于事。
check.php
<?php
$username = (isset($_GET['username']) ? $_GET['username'] : null);
$john = "John";
$fileName = "users.txt"; // get the text file
$users = file_get_contents( $fileName ); // text file to string
$objUsers = json_decode( $users ); // string to object
// iterate through the list and find John
for( $i = 0; $i < count($objUsers); $i++ ){
if(// what to do here?){
echo '<div>John is found</div>';
} else {
echo '<div>John is NOT found</div>';
break;
}
}
?>
作为一个OOP人,我发现像
这样的条件是合乎逻辑的if( $objUsers[$i] == $john)
但似乎这种方法在php中失败了,因为它总是屈服于#34; John找不到&#34;即使John确实存在于列表中。我做错了什么?谢谢