2

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>
12
  • There are no matching, so what you want to do? Commented May 28, 2016 at 11:50
  • you don't have a key called 'value' in your second array. ($second['value'] will be null or rather an error 'undefined index') Commented May 28, 2016 at 11:51
  • Apart from that: what value should match with which key? There's nothing that could match. Or is there other data you didn't show? Commented May 28, 2016 at 11:54
  • @Frayne i update my code can you explain please what is my mistake Commented May 28, 2016 at 11:57
  • I can't see your updates.. Commented May 28, 2016 at 11:58

2 Answers 2

6

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";
    }
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Frayne Konok
0

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";
    }
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.