PHP foreach loop inside foreach loop?
16,209
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";
}
}
}
Related videos on Youtube
Comments
-
sandip kakade almost 2 years
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>
-
sandip kakade almost 8 yearsThanks @Frayne Konok