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

时间:2022-09-22 23:56:27

I want to loop through this array:

我想遍历这个数组:

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

$pagename="admin.php"

Then if admin.php is found then execute this code:

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

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

How would I put together this looping statement?

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

7 个解决方案

#1


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

#2


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

#3


I am thinking this might do what you want to do...

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

$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();
}

#4


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

#5


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

#6


just in case you wanted to know how to actually loop through an array.

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

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

  //$value is an item in the array.

}

#7


check out for and if

检查,如果

#1


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

#2


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

#3


I am thinking this might do what you want to do...

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

$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();
}

#4


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

#5


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

#6


just in case you wanted to know how to actually loop through an array.

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

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

  //$value is an item in the array.

}

#7


check out for and if

检查,如果