简体   繁体   English

in_array()期望参数2为数组

[英]in_array() expects parameter 2 to be array

I have this idea of an multiarray with my files sorted into different groups/arrays. 我有一个多数组的想法,我的文件分为不同的组/数组。 Thinking it would be the easiest and most clean way to check what file we're inside, and should the navigation state be set to active on navigation tab 1,2 or 3? 认为这是检查我们里面的文件的最简单,最干净的方法,并且应该在导航选项卡1,2或3上将导航状态设置为活动状态吗?

I have this array; 我有这个数组;

Array
(
[home] => Array
    (
        [0] => index.php
        [1] => something.php
    )

[tour] => Array
    (
        [0] => tour.php
    )

[tutorials] => Array
    (
        [0] => tutorials.php
    )
)

The idea is, if i click on the home navigation button it goes to the index.php and a function checks whether it's the Home button that needs the active state, or the Tour button. 这个想法是,如果我单击主页导航按钮,它将转到index.php,然后一个函数检查是需要活动状态的“主页”按钮还是“游览”按钮。 In this case its the Home button. 在这种情况下,其为“主页”按钮。

[home] => Array
    (
        [0] => index.php

I made this function 我做了这个功能

function findNavigationActive($tap, $filename) {
    if(in_array($filename, $topNavigationPages[$tap])) {
    return 1;
}
return 0;
}

it should check, if index.php ($filename) is in the Home ($tap) array or navi-tap, then return 1. Unfortunately i get this error insted; 它应该检查,如果index.php($ filename)位于Home($ tap)数组或navi-tap中,则返回1。不幸的是,我收到此错误;

Warning: in_array() expects parameter 2 to be array, null given in /project/../../topNavigationHandler.php on line 21 警告:in_array()期望参数2为数组,在第21行的/project/../../topNavigationHandler.php中给出的值为null

What am I doing wrong? 我究竟做错了什么?

FIXED 固定

Used global $topNavigationPages; 已使用的全局$ topNavigationPages; outside the function, and set $topNavigationPages values (array) beneath, and again used global $topNavigationPages inside the function to pull the array inside. 在函数外部,并在下面设置$ topNavigationPages值(数组),然后再次在函数内部使用全局$ topNavigationPages将数组拉入内部。 Thanks @tombs ! 谢谢@tombs!

What this error means is that the second parameter isn't an array, therefore you can try something like this.: 该错误表示第二个参数不是数组,因此您可以尝试如下操作:

if (is_array($topNavigationPages[$tap])) {
    if(in_array($filename, $topNavigationPages[$tap])) {
        return 1;
    }
}
return 0;

You can do this without nesting the if loops by using a strict and operator. 您可以通过使用strict和运算符来执行此操作而无需嵌套if循环。

Hope this helps 希望这可以帮助

Note: To resolve your scope issue you can use either a global value or a constant. 注意:要解决范围问题,可以使用全局值或常量。 To use a constant use the 'define' function, for a global declare the variable as such at the beginning of your file and in every subsequent function that you use it in. I strongly recommend using a constant. 要使用常量,请使用'define'函数,在文件的开头以及使用该常量的每个后续函数中都对变量进行全局声明。我强烈建议使用常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM