[英]PHP foreach loop inside foreach loop?
I have two foreach loop. 我有两个foreach循环。 In first foreach loop menu list and second foreach loop is database fetch loop.
在第一个foreach循环菜单列表中,第二个foreach循环是数据库访存循环。 So i want compare first foreach key to second foreach value first foreach array result is shown
所以我想比较第一个foreach键与第二个foreach值,然后显示第一个foreach数组结果
Array
(
[master/city] => City
[master/national_holiday] => National Holiday
[master/operator_comments] => Operator Comments
[master/sensors] => Sensors
)
and second foreach array result 和第二个foreach数组结果
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.您可能会寻找第二个数组的
menu_url
和第一个数组的键的关系。
For this you need to use $second['menu_url']
in the second array. 为此,您需要在第二个数组中使用
$second['menu_url']
。
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: 如果通过$ second_array值检查是否匹配。使用此代码:
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: 如果只检查['menu_url']与array的匹配。使用此代码:
foreach( $first_array as $key => $value) {
foreach( $second_array as $second ){
if ($second['menu_url'] == $key) {
echo "Hi";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.