簡體   English   中英

PHP-如何通過從GET接收的鍵獲取數組值

[英]PHP - how to get array value by key received from GET

如何遍歷所有短語並使用GET參數將它們輸出為鏈接列表?

如何制作一個以整數索引的數組,然后使用GET變量選擇這些短語之一。

到目前為止,我的代碼;

$arr = array('1' => 'Que será, será.', '2' => 'C’est la vie!', '3' => 'Sich nicht um ungelegte Eier kümmern.', '4' => 'Ada asap, ada api.', '5' => 'Batti il ferro finché è caldo.');
?> 

<p><?php print $arr[1]; ?> - Whatever will be, will be.</p>
<p><?php print $arr[2]; ?> – That’s the life.</p> 
<p><?php print $arr[3]; ?> - Don’t count your chickens before they hatch.</p> 
<p><?php print $arr[4]; ?> - There's no smoke without fire.</p> 
<p><?php print $arr[5]; ?> - Strike while the iron is hot.</p>

應該這樣做:

$key = (int)$_GET['key'];
if (isset($arr[$key]) {
   echo print $arr[$key]; 
} else {
   echo "Invalid option specified";
}
<?php  
  $key = mysql_real_escape_string($_GET['value']);//Pass this value from url and sanitize the inputs
  ?>
  <p><?php print $arr[$key]; ?>
$key = $_GET['index'];
$val = isset($arr[$key]) ? $arr[$key] : false;

編輯:為什么人們不斷推薦mysql_real_escape_string? 這用於數據庫查詢轉義,並向數據庫發送請求(據我所知)。 密鑰甚至不需要轉義,因為它沒有被發送到數據庫或輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM