2016-07-18 40 views
0

所以我有一個數組和表單,其中輸入了一個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的正確的信息,但它也表明「人未找到」的每一個其他人在數組中。我怎樣才能讓它只顯示與輸入相匹配的人?

+2

請注意,除非你已經明確定義了'status'和'name'作爲常量,否則你會得到很多php警告。最好使用引號'$ person [「status」]' – rjdown

+0

Grabacr,能否請您提供一個關於您的問題在哪裏的更新?您是否可以使用這些答案之一來幫助解決您的問題? – anFfWuy7qbJWaPp6DSOR

回答

4

沒有必要在所有的循環。如果你已經擁有的人的ID,那麼就沒有必要掃描該ID的陣列,只需直接使用它:

if (isset($people[$id])) { 
    ... do stuff with person # $id 
} else { 
    ... person does not exist 
} 
-2

使用一個布爾變量來檢測,如果你正在尋找的人被發現還是不行,代碼可能是這樣的:

$found = false; 
foreach($people as $id => $person) { 
    if($_GET[person]==$id) { 
     $found = true; 
     if($person[status]==1) 
      echo $person[name] . "(" . $id . "): ready"; 
     else if($person[status]==0) 
      echo $person[name] . "(" . $id . "): not ready"; 
     //You can (break;) here if you're sure that the $id is identical 
    } 
} 
if($found == false) 
    echo "Person with this ID was not found"; 
1

由於您只尋找一個「人ID」,你不需要在你的代碼中的第二個foreach循環。我建議重構一下,以包含更多的錯誤檢查,並閱讀關於PHP中的type juggling and casting

我修改你的代碼,包括一些暗示,應該指向你在正確的方向:

<?php 

$file = 'people.txt'; 
$people = array(); 

$ref = fopen($file, 'r'); 

while ($row = fgets($ref, 1024)) { 
    $row = explode('|', $row); 

    if (!isset($row[0]) || !isset($row[1]) || !isset($row[2])) { 
     continue; 
    } 

    $id = (int) $row[0]; 
    $name = (string) $row[1]; 
    $status = (int) $row[2]; 

    $people[$id] = array('name' => $name, 'status' => $status); 
} 

fclose($ref); 

$id = array_key_exists('person', $_GET) ? $_GET['person'] : null; 

if (array_key_exists($id, $people)) { 
    $person = $people[$id]; 

    if ($person['status'] === 1) { 
     echo $person['name'] . ' (' . $id . '): ready'; 
    } else { 
     echo $person['name'] . ' (' . $id . '): not ready'; 
    } 
} else { 
    echo 'Person with ID ' . htmlspecialchars($id) . ' not found'; 
} 

如果你打算people.txt變大的規模,考慮存儲在數據庫等數據作爲MySQL。這將消除在每次請求時必須將文件的內容讀入內存。

1

除非在此代碼後您需要所有人用於其他目的的陣列,否則您只能從文件中獲得$_GET['person']指定的人員。只需在while循環內添加一個檢查。

$person = null;  // initialize person 

$ref = fopen($file, 'r'); 
while ($row = fgets($ref, 1024)) { 
    $row = explode("|", $row); 
    if ($row[0] == $_GET['person']) { // only get rows that match the searched id 
     $person = array('name' => $row[1], 'status' => $row[2]); 
    } 
} 
fclose($ref); 

這將通過避免創建整個文件的數組來減少腳本的內存使用量。這樣做的好處顯然取決於文件的大小。

(即使你需要整個陣列,您仍然可以在你的while循環if塊設置$person同時你盡顯$people陣列。)

這個循環,然後進行, $person將只是一件事:或者是文件中最後一個匹配行的數組,或者null。這意味着你不必在數組中找到它;你可以輸出這樣的狀態信息:

if ($person) { 
    echo "$person[name] ($person_id): "; 
    echo $person['status'] ? 'ready' : 'not ready'; 
} else { 
    echo "Person with this ID was not found"; 
}