如何在php中循环遍历此数组

时间:2009-06-10 19:31:41

标签: php arrays loops

我想遍历这个数组:

$securePages=array("admin.php","addslot.php","classpost.php");

$pagename="admin.php"

然后,如果找到admin.php,则执行以下代码:

header("location:index.php");
exit();

我如何将这个循环语句放在一起?

7 个答案:

答案 0 :(得分:14)

if (in_array("admin.php", $securePages)) {
    header("location:index.php");
    exit();
}

答案 1 :(得分:6)

if (in_array($pagename, $securePages)) {
    header("Location: http://example.com/index.php");
    exit();    
}

答案 2 :(得分:3)

我想这可能会做你想做的事情......

$securePages = array("admin.php","addslot.php","classpost.php");
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$url = parse_url($url);
$path = $url['path']; // bar.php

if (in_array($path, $securePages)) {
    header("location:index.php");
    exit();
}

答案 3 :(得分:2)

if (in_array($pagename,$securePages)) {
  header("location:index.php");
 exit();
}

答案 4 :(得分:1)

  foreach($securePages AS $page)
  {
      if ($page == "admin.php")
      {
           header("location:index.php");
           exit();
      }
  }

答案 5 :(得分:1)

以防你想知道如何实际循环数组。

$securePages=array("admin.php","addslot.php","classpost.php");
foreach ($securePages as $value) {

  //$value is an item in the array.

}

答案 6 :(得分:0)

结帐forif