【发布时间】:2015-07-20 21:43:53
【问题描述】:
我想检查页面名称是否是包含文件中定义的数组中的值,如果是,则包含它。
<?php
// parent page that includes the files
$pageLabel = 'three';
if (in_array('pageLabel', $multicats)) {
include $filename;
}
?>
<?php
// example for a file to be included
$multicats = array('one', 'three', 'five');
$filename = $_SERVER['PHP_SELF'];
echo 'Hello World of one, three and five';
?>
然而,一个错误表明它期望 $multicats 是一个数组,它确实是一个数组,这意味着它不会检查包含的文件。
怎么办?
【问题讨论】:
-
你为什么要这样做?检查一个值是否在一个文件中的数组中没有多大意义,如果该值在数组中,您只想包含该文件
-
您是否包括在 中定义了数组的文件?
-
是的!目录中的每个文件都有一个数组。如果该数组中的某个值与父页面上定义的变量匹配,则应将其包含在内。