0

I have an XML file which I read into a PHP array for processing using CakePHP's XML::toArray($xmlString) function.

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.

Here's the if statement:

$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 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. 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?

2

2 Answers 2

2

First argument of function in_array is the needle and second argument is the array to search for match!!

Reference: PHP Manual

Sign up to request clarification or add additional context in comments.

Comments

1
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
  }
}

2 Comments

Why the (array)? It's not needed and might result in very weird results because of confertions. If it is to make sure it is an array, check for an array. IMO proper coding doesnt need that, just make sure all userinput gets handled the correct way :)
I know it may look "bulky" but IMO proper coding does need that. Nothing (at all) is lost, and since the second parameter to in_array() always needs to be an array, putting (array) in front of the variable ensures that if a non-array accidentally gets passed to it, it will be converted into array, thereby preventing unnecessary errors from appearing in the error log, unnecessary broken code, etc. If the variable passed to it is already an array, it will be left alone. It does nothing but help. But I do agree that user input should be handled correctly. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.