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?