这是我的数组的print_r
Array (
[0] => Array ( [displayorder] => 3 [menuname] => Accounting Module [menuurl] => [menuflag] => 1 [menuid] => 3 )
[1] => Array ( [displayorder] => 4 [menuname] => My Profile [menuurl] => myprofile.php [menuflag] => 0 [menuid] => 4 )
[2] => Array ( [displayorder] => 6 [menuname] => HR Module [menuurl] => [menuflag] => 1 [menuid] => 2 )
[3] => Array ( [displayorder] => 9 [menuname] => Administrator [menuurl] => [menuflag] => 1 [menuid] => 1 ) )
我想提出一个条件,当我有一个menuid
输入时,它将获得menuname
menuid
找到了这个解决方案:
来自here 的 $myarray[0]['menuname'];
但它只获得第一个数组的menuname
,你怎么能让它变得动态?
答案 0 :(得分:1)
创建一个函数,将多数组和菜单ID作为参数传递给它,从多数组中返回所需的值,如:
function getMenuNameById($yourArr, $menuId) {
foreach($yourArr as $val) {
if( $val["menuid"] == $menuId ) {
return $val["menuname"];
}
}
}
//use it
$menuName = getMenuNameById($your_multi_arr, $menu_id);