php检查数组是否有子数组不工作

时间:2022-06-15 00:58:47

I am trying to check if array has subarray or not. But it doesn't work. For example, though It is checked main which has subarray, it echo, it hasn't submenu What is wrong here ?

我试图检查数组是否有子阵列。但它不起作用。例如,虽然检查主要哪个有子阵列,但它回显,它没有子菜单这里有什么问题?

<li>
    <input type="checkbox" name="menu[]" value="main">
    <ul>
        <li><input type="checkbox" name="menu[0][]" value="sub-main1"></li>
        <li><input type="checkbox" name="menu[0][]" value="sub-main2"></li>
        <li><input type="checkbox" name="menu[0][]" value="sub-main3"></li>
     </ul>
</li>

<li><input type="checkbox" name="menu[]" value="sport"></li>
<li><input type="checkbox" name="menu[]" value="art"></li>

PHP

PHP

foreach ($_POST["menu"] AS $key => $item){
    if (is_array($item)){
        echo "It has submenu";
    }
    else{
        echo "it hasn't submenu";
    }
}

1 个解决方案

#1


0  

It works while changing

它在改变时起作用

if (is_array($item)){

to

if(is_array($_POST["menu"][$key])){

#1


0  

It works while changing

它在改变时起作用

if (is_array($item)){

to

if(is_array($_POST["menu"][$key])){