1

Seems weird matter. Is there any way to detect array that contains sub-array in php, such parent-child concept in css, jquery. I want to insert data only if array has subarray

Dynamic Menu

<?php
    if(){ //loop starts
?>  
    <li>
        <input type="checkbox" name="menu[]" value="<?php echo $row['value']?>"> <?php echo $row['title'];
            <ul>
                <?php
                    if(){ //loop starts
                ?>
                <li>
                    <input type="checkbox" name="submenu[]" value="<?php echo $row1['value']?>"><?php echo $row1['subtitle'];
                </li>;
                }?>

            </ul>
    </li>
}?>

Output

<li>
    <input type="checkbox" name="menu[]" value="vehicle">
    <ul>
        <li><input type="checkbox" name="submenu[]" value="car"></li>
        <li><input type="checkbox" name="submenu[]" value="bike"></li>
     </ul>
</li>
<li><input type="checkbox" name="menu[]" value="sport"></li>

upload.php

$data = explode("," ,$_POST["menu"]); 
$subdata = explode("," ,$_POST["submenu"]);
foreach ($data AS $key => $menu){
    if(){         // This Menu has subarray

    }
Dipak
  • 931
  • 14
  • 33

2 Answers2

3

You can use the is_array function to see if a variable is an array, if you want to recursively check then you can use array_walk_recursive with is_array as the callback.

Note: If you use array_walk_recursive, it will run the callback on items that are non array, so you could compare that which is returned from array_walk_recursive against the original array to find out all those items which are arrays. However, is_array seems like the easier option.

Live Example

Repl

Script47
  • 14,230
  • 4
  • 45
  • 66
  • thanks, I had used is_array function, but it considers one's `sub array` as `common subarray`. For example in my question one menu has submenu, another hasnot. `if(is_array($data)){}` is inserting data in both array that contain `subarray` and `non subarray`. I'm new in `array_walk_recursive`. Please provide any demo using this, hope this will helps – Dipak Jun 06 '18 at 17:28
1

in your case you have to do easy with following method

<li>
        <input type="checkbox" name="menu[0][]" value="vehicle">
        <ul>
            <li><input type="checkbox" name="submenu[0][]" value="car"></li>
            <li><input type="checkbox" name="submenu[0][]" value="bike"></li>
         </ul>
    </li>
    <li><input type="checkbox" name="menu[1][]" value="sport"></li>
        <ul>
            <li><input type="checkbox" name="submenu[1][]" value="cricket"></li>
            <li><input type="checkbox" name="submenu[1][]" value="football"</li>
         </ul>

then from a backend, you can do like

foreach($_POST['menu'] as $key=>$val) {
        if(is_array($val) && count($val)) {
           //do action
        }
}
BrahmWeb
  • 36
  • 2
  • seems it will easy and best answer, but I have dynamic list generated using loop , Please help how to increase number i.e. `menu[0][], menu[1][]....menu[5][]` as output in your answer. Please see my updated question I put my dynamic code – Dipak Jun 06 '18 at 16:57