简体   繁体   English

in_array函数不起作用

[英]in_array function not working

I have an XML file which I read into a PHP array for processing using CakePHP's XML::toArray($xmlString) function. 我有一个XML文件,我将其读入PHP数组以使用CakePHP的XML :: toArray($ xmlString)函数进行处理。

Everything works fine and I can easily work with the data, however, when I try to run an in_array check, it keeps on returning false, even though I know the value is there. 一切正常,我可以轻松地处理数据,但是,当我尝试运行in_array检查时,即使知道值存在,它仍会继续返回false。

Here's the if statement: 这是if语句:

$xmlArray = Xml::toArray($xmlString);
$menus = $xmlArray['navigation'];
$groupId = array($groupId); // Group ID of logged in user, in my case 1
$loggedInId = array($loggedInId); // User ID, in my case 182
//I change them both into arrays, as the second param needs to be an array, and in the XML file, there could only be one value, in which case it's "translated" as a string
//$submenu == foreach($menu['submenu']['menu'] as $subenu)
if(in_array($submenu['permission']['group'],$groupId) || in_array($submenu['permission']['user'],$loggedInId)) {
    //do stuff
}

Here's the XML file (the navigation bit): 这是XML文件(导航位):

<?xml version="1.0" encoding="utf-8"?>
<navigation>
    <menu>
        <title>Home</title>
        <url>home</url>
        <submenus>
            <menu>
                <title>In memory of...</title>
                <url>memoriams</url>
                <image>memoriam</image>
                <permission>
                    <group>1</group>
                    <user>0</user>
                </permission>
            </menu>
            <menu>
                <title>Reports</title>
                <url>reports</url>
                <image>report</image>
                <permission>
                    <group>4</group>
                    <group>5</group>
                    <user>252</user>
                    <user>182</user>
                    <user>234</user>
                </permission>
            </menu>
        </submenus>
    </menu>
</navigation>

The menu only shows up in the first case. 菜单仅在第一种情况下显示。 That is if group/user only have 1 value. 也就是说,如果组/用户只有1个值。 If it has more than one value, it just doesn't show the sub menu. 如果它具有多个值,则仅不显示子菜单。

Here is a debug of the array: 这是数组的调试:

array(
    'menu' => array(
        'title' => 'Home',
        'url' => 'home',
        'submenus' => array(
            'menu' => array(
                (int) 0 => array(
                    'title' => 'In memory of...',
                    'url' => 'memoriams',
                    'image' => 'memoriam',
                    'permission' => array(
                        'group' => '1',
                        'user' => '0'
                    )
                ),
                (int) 1 => array(
                    'title' => 'Reports',
                    'url' => 'reports',
                    'image' => 'report',
                    'permission' => array(
                        'group' => array(
                            (int) 0 => '4',
                            (int) 1 => '5'
                        ),
                        'user' => array(
                            (int) 0 => '252',
                            (int) 1 => '182',
                            (int) 2 => '234'
                        )
                    )
                )
            )
        )
    )
)

Does anyone have any idea why it does not want to work? 有谁知道为什么它不想工作?

First argument of function in_array is the needle and second argument is the array to search for match!! 函数in_array第一个参数是needle ,第二个参数是要搜索匹配项的array

Reference: PHP Manual 参考: PHP手册

foreach ($menu['menu']['submenus']['menu'] as $submenu) {
  $group = $submenu['permission']['group'];
  $user = $submenu['permission']['user'];
  if(in_array($group, (array) $groupId) || in_array($user, (array) $loggedInId)) {
    //do stuff
  }
}

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

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