所以我有一個數組和表單,其中輸入了一個ID,應該顯示數組中相應的行。如何從PHP數組中獲得指定的行
到目前爲止,我已經做出這樣的:
<?php
$file = 'people.txt';
$people = array();
$ref = fopen($file, 'r');
while ($row = fgets($ref, 1024)) {
$row = explode("|", $row);
$id = $row[0];
$name = $row[1];
$status = $row[2];
$people[$id] = array('name' => $name, 'status' => $status);
}
fclose($ref);
foreach($people as $id => $person) {
if($_GET[person]==$id && $person[status]==1) {
echo $person[name] . "(" . $id . "): ready";
}
if($_GET[person]==$id && $person[status]==0) {
echo $person[name] . "(" . $id . "): not ready";
}
if($_GET[person]!=$id) {
echo "Person with this ID was not found";
}
}
?>
這表明搜索ID的正確的信息,但它也表明「人未找到」的每一個其他人在數組中。我怎樣才能讓它只顯示與輸入相匹配的人?
請注意,除非你已經明確定義了'status'和'name'作爲常量,否則你會得到很多php警告。最好使用引號'$ person [「status」]' – rjdown
Grabacr,能否請您提供一個關於您的問題在哪裏的更新?您是否可以使用這些答案之一來幫助解決您的問題? – anFfWuy7qbJWaPp6DSOR