简体   繁体   English

PHP-如何通过从GET接收的键获取数组值

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

How do I Loop through all phrases and output them as a list of links, with GET-parameters? 如何遍历所有短语并使用GET参数将它们输出为链接列表?

How do I make an array, indexed with integers then use a GET-variable to choose one of these phrases. 如何制作一个以整数索引的数组,然后使用GET变量选择这些短语之一。

My code so far; 到目前为止,我的代码;

$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>

This should do it: 应该这样做:

$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;

Edit: Why do people keep recommending mysql_real_escape_string? 编辑:为什么人们不断推荐mysql_real_escape_string? This is for database query escaping and sends a request to the database (as far as I understand). 这用于数据库查询转义,并向数据库发送请求(据我所知)。 The key doesn't even need escaping as it's not being sent to the database or output. 密钥甚至不需要转义,因为它没有被发送到数据库或输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM