我有一個XML文件,我讀入PHP數組進行處理,使用CakePHP的XML :: toArray($ xmlString)函數。in_array函數不工作
一切工作正常,我可以輕鬆地處理數據,但是,當我嘗試運行in_array檢查時,即使我知道該值是否存在,它仍會繼續返回false。
這裏的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
}
這裏的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>
菜單僅在第一種情況下顯示出來。這是如果組/用戶只有1個值。如果它有多個值,它只是不顯示子菜單。
這裏是陣列的調試:
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'
)
)
)
)
)
)
)
沒有人有任何想法,爲什麼它不想工作?
你剛剛得到的參數以錯誤的方式... – Clive
也看這裏http://php.net/manual/en/function.in-array.php –