1

I'm trying to get text from multi array, I got 1st and 2nd array but could not to get text from 3rd array. You can see my code here:

<div class="personTools">
    <ul>
        <?php
            for ($i = 0; $i < count($toolsMenu["TOOLS_MENU"]) ; $i++){
        ?>
        <div class="dropdown">
            <li><?php echo $toolsMenu["TOOLS_MENU"][$i]; ?> <span class="fa fa-caret-down"></span></li>
            <div class="dropdown-content">
                <?php
                    for ($d = 0; $d < count ($toolsMenu["TOOLS_MENU"][$i]); $d++) {
                ?>
                        <li><?php echo $toolsMenu["TOOLS_MENU"][$i][$d]; ?> </li>
                <?php
                    }
                ?>
            </div>
        </div>
        <?php
            }
        ?>
    </ul>
</div>

and my array here:

$toolsMenu = array(
    "TOOLS_MENU" => array(
            "تجربة 1" => array(1, 2, 3, 4),
            "تجربة 2" => array(1, 2, 3, 4),
            "تجربة 3" => array(1, 2, 3, 4),
            "تجربة 4" => array(1, 2, 3, 4)
     )
);

My quastion is: Why I'm getting this message?

Notice: Undefined offset: 0 in C:\wamp64\www\mazadi\tmpl\html.tpl on line

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Mousa Saleh
  • 42
  • 1
  • 10

1 Answers1

2

When foreach() is given then why to use for():-

<div class="personTools">
    <ul>
        <?php
            foreach ($toolsMenu["TOOLS_MENU"] as $key=> $toolsM){
        ?>
        <div class="dropdown">
            <li><?php echo $key; ?> <span class="fa fa-caret-down"></span></li>
            <div class="dropdown-content">
                <?php
                    foreach ($toolsM as $tools) {
                ?>
                        <li><?php echo $tools; ?> </li>
                <?php
                    }
                ?>
            </div>
        </div>
        <?php
            }
        ?>
    </ul>
</div>

Note:- Try to avoid for loop as much as possible, if you are able to handle things with foreach()because foreach() take care of indexes itself, while for loop doesn't.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98