Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将if/else if放入数组中?_Php_Arrays_Request_Uri - Fatal编程技术网

Php 如何将if/else if放入数组中?

Php 如何将if/else if放入数组中?,php,arrays,request,uri,Php,Arrays,Request,Uri,我必须在一些url中添加noindex,这是我目前的代码: <?php if ($metanoindex) { ?> <meta name="robots" content="noindex,follow" /> <?php } elseif ($_SERVER['REQUEST_URI']=='/dolls/1/bestselling' ) { ?> <meta name="robots" content="noindex,follow" />

我必须在一些url中添加noindex,这是我目前的代码:

<?php if ($metanoindex) { ?>
<meta name="robots" content="noindex,follow" />
<?php } elseif ($_SERVER['REQUEST_URI']=='/dolls/1/bestselling' ) { ?>
<meta name="robots" content="noindex,follow" />
<?php } elseif ($_SERVER['REQUEST_URI']=='/dolls/1/bestselling/30' ) { ?>
<meta name="robots" content="noindex,follow" /> 
<?php } elseif ($_SERVER['REQUEST_URI']=='/dolls/1' ) { ?>
<meta name="robots" content="noindex,follow" /> 
<?php } else { ?>
<meta name="robots" content="index,follow" />
<?php } ?>

我想知道是否有一种方法可以创建一个URL数组,而不是每次都重复“else if”。

试试下面的方法

<?php
    $arr = array('/dolls/1/bestselling','/dolls/1/bestselling/30','/dolls/1');
    if($metanoindex || in_array($_SERVER['REQUEST_URI'],$arr)){ ?>
      <meta name="robots" content="noindex,follow" /> 
<?php
    }
    else {
?>
      <meta name="robots" content="index,follow" />
<?php } ?>


PHP手册:

看看函数n在PHP的数组中:改用
if($metanoindex | |在数组中($服务器['REQUEST_URI',$array))
。。再看看这个问题,好的。。这就是为什么我删除了我的答案以避免重复
$urls = array('/dolls/1/bestselling', '/dolls/1/bestselling/30', '/dolls/1');
if (in_array($_SERVER['REQUEST_URI'], $urls)) {
    // Insert meta tag here
}