简体   繁体   中英

PHP foreach loop inside foreach loop?

I have two foreach loop. In first foreach loop menu list and second foreach loop is database fetch loop. So i want compare first foreach key to second foreach value first foreach array result is shown

   Array
(
    [master/city] => City
    [master/national_holiday] => National Holiday
    [master/operator_comments] => Operator Comments
    [master/sensors] => Sensors
)

and second foreach array result

   Array
        (
            [0] => Array
                (
                    [menu_url] => monitoring/tickets
                    [menu_category] => monitoring
                    [read] => 1
                    [write] => 1
                )

            [1] => Array
                (
                    [menu_url] => monitoring/serach_tickets
                    [menu_category] => monitoring
                    [read] => 1
                    [write] => 1
                )

            [2] => Array
                (
                    [menu_url] => master/national_holiday
                    [menu_category] => monitoring
                    [read] => 1
                    [write] => 0
                )
)

I try to use this code but not working fine

   foreach( $first_array as $key => $value) {

  foreach( $second_array as $second ) {
    if ($second['value'] == $key) {
       echo "Hi";
    }
   }
}

can you suggest what is my mistake.

My real code using in view

<?php
                    $i = 1;

                    foreach($first_array as $k => $val) {
                    ?>
                    <tr>
                      <td>{{ $i }}</td>
                      <td class="mailbox-name">{{ $val }}</td>
                    <?php
                        foreach ($edit_rights['role_rights'] as $rights) {
                    ?>                                           
                      <td><input type="checkbox" class="master_read" name="menu_master_read[]" <?php if ($rights['menu_url'] == $k) { echo 'checked'; } else {echo ''; }?> value="{{ $k }}"></td>
                      <td><input type="checkbox" class="master_write" name="menu_master_write[]" value="{{ $k }}"></td>
                    </tr>
                     <?php } $i++;  } ?>
                    </tr>

As i don't see any relation between your arrays, But i can guess what you are looking for.

You may looking for the ralation of second array's menu_url and first array's key.

For this you need to use $second['menu_url'] in the second array.

foreach( $first_array as $key => $value) {
  foreach( $second_array as $second ){
    if ($second['menu_url'] == $key) {
       echo "Hi";
    }
   }
}

if check matching on by on $second_array value.use this code:

foreach( $first_array as $key => $value) {
      foreach( $second_array as $second=>$val ) {
          foreach($val as $check=>$check_value){
                //var_export($check);echo "\n";
                //var_export($key);echo "\n";
                //var_export($second_array[$second][$check]);echo "\n";
                //var_export($second_array[$second][$check] == $key);echo "\n";
                //echo"======================= \n";
                if ($second_array[$second][$check] == $key) {
                   echo "Hi";
                }
            }
       }
}

if just check ['menu_url'] matching to array.use this code:

foreach( $first_array as $key => $value) {
  foreach( $second_array as $second ){
    if ($second['menu_url'] == $key) {
       echo "Hi";
    }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM