【问题标题】:php if $_GET equals a certain arrayphp 如果 $_GET 等于某个数组
【发布时间】:2011-09-21 06:57:04
【问题描述】:

我正在尝试使用 break 概念,但我也在使用一些内置的 PHP 函数来只允许某些值进入 get 函数。

    $allowedKeys = array(
          'route'
);

$options = array(
    'chapter_1' => 'chapter_1',
    'chapter_2' => 'chapter_2',
    'chapter_3' => 'chapter_3'
);

$_GET = array_intersect_key($_GET, array_flip($allowedKeys));

if($_GET[$allowedKeys[0]] && array_key_exists($_GET[$allowedKeys[0]], $options)) {
    if($_GET[$allowedKeys[0]] == $options[0]) {
        /* This is where I'm trying to see if route=chapter_1 then do something.
           The logic I'm trying to write is if the route is chapter_1 then print
           out the content from chapter 1 How can determine this? */

        echo "Hello";
    }

}

为什么这段代码没有回显“hello”?

【问题讨论】:

  • $_GET 数组中有更多的键为什么重要?如果你不想要它们,那就不要使用它们。
  • 这怎么应该投反对票?
  • 可能是因为问题不清楚。为什么$options是一个键值相等的数组?
  • 问题显然很清楚。

标签: php arrays get


【解决方案1】:

为什么要让它变得比需要的更复杂?

//check you have a route
$route = isset( $_GET['route'] ) ? $_GET['route'] : '';

switch( $route ) {
    case 'chapter_1':
        //do chapter one stuff
        echo 'Chapter 1';
        break;
    case 'chapter_2':
        //do chapter two stuff
        echo 'Chapter 2';
        break;
    default:
        echo 'Intro';
}

【讨论】:

    【解决方案2】:

    我会直接为你回答你的问题。 'Hello' 未显示,因为它位于未触发的 if 语句中,因为 "if($_GET[$allowedKeys[0]] && array_key_exists($_GET[$allowedKeys[0]], $options) )" 或 "if($_GET[$allowedKeys[0]] == $options[0])" 返回 false。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-26
      • 2011-12-06
      • 2010-12-22
      • 2012-05-25
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多