循环遍历2个数组以找出应选中的复选框 [英] looping through 2 array to find out which checkboxes should be checked

查看:79
本文介绍了循环遍历2个数组以找出应选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数组,$ categories_filters和$ adds_filters。他们都返回结果。当使用print_r打印时,它们以以下格式返回数据:

I have 2 arrays, $categories_filters and $adds_filters. They both return results. When printed using print_r, they return data in following format:

$ categories_filters返回如下数据:

$categories_filters returns data like this:

Array
(
[0] => Array
    (
        [filterid] => 67
        [catid] => 1
        [filtername] => FILTERNAME1
        [sorder] => 1
        [visible] => 1
    )

[1] => Array
    (
        [filterid] => 68
        [catid] => 1
        [filtername] => FILTERNAME155
        [sorder] => 2
        [visible] => 1
    )
    .....

$ adds_filters返回以下内容:

$adds_filters returns the following:

  Array
(
[0] => Array
    (
        [addfilterid] => 9
        [addid] => 5
        [filterid] => 67
    )

[1] => Array
    (
        [addfilterid] => 10
        [addid] => 5
        [filterid] => 163
    )

)....

我正在尝试以下内容:
我在$ categories_filters中的每个值都有一个复选框。如果$ ids_filters数组中存在filterid,则我希望选中该复选框,否则,我希望该复选框仅显示为未选中。

I am trying the following: I have checkbox for each value in the $categories_filters. If the filterid exists in the $adds_filters array, i want that checkbox to be checked, otherwise, i want that checkbox just to be displayed unchecked.

我正在尝试实现使用以下代码:

I am trying to achieve that with the following code:

if($categories_filters) {
    foreach ($categories_filters as $key1=>$value){
        echo "<div class='chb_group'>";
        echo "<span class='custom_chb_wrapper'>";
        foreach ($adds_filters as $key2=>$af) {
            if($af['filterid'] == $value['filterid']) {
                echo "<input type='checkbox' name='categoriesfilters[]' id= '".$value['filterid']."'  value='" .$value['filterid'] ."'  checked = 'checked' class='zcheckbox' />";    
            } else  {
                echo "<input type='checkbox' name='categoriesfilters[]' id= '".$value['filterid']."'  value='" .$value['filterid'] ."' class='zcheckbox' />";
            }
        } 
        echo "<label>" .$value['filtername']. "</label>";
        echo "</span>";
        echo "</div>"
    } 
}else {
    echo  "No filters"; 
}

快速捕获器将意识到我为数组中的每个值显示了2个复选框,而不是一个(选中或未选中的)

Quick catchers will realize that I get 2 checkboxes displayed for each value in the arrays, instead of one (checked or unchecked)

我想这里需要使用其他方法。

I guess that different approach is needed here.

推荐答案

一个建议是,不要每次都遍历整个 $ adds_filters 数组,而是存储所有'filterid'放入变量,然后检查该数组中是否存在 $ value ['filterid']

下面的代码使用函数 in_array array_map

尝试-

One suggestion would be that instead of looping through the entire $adds_filters array everytime, store all the 'filterid' into a variable and just check if the $value['filterid'] exists inside that array.
The code below uses functions in_array and array_map.
Try this -

if($categories_filters) {
    //Added this
    $addfilterids = array_map(function($v){return $v['filterid'];}, $adds_filters);

    foreach ($categories_filters as $key1=>$value){
        echo "<div class='chb_group'>";
        echo "<span class='custom_chb_wrapper'>";

        //Modified from here-
        if(in_array($value['filterid'], $addfilterids)){
            echo "<input type='checkbox' name='categoriesfilters[]' id= '".$value['filterid']."'  value='" .$value['filterid'] ."'  checked = 'checked' class='zcheckbox' />";    
        }else{
            echo "<input type='checkbox' name='categoriesfilters[]' id= '".$value['filterid']."'  value='" .$value['filterid'] ."' class='zcheckbox' />";
        }
        //^Modified uptil here.

        echo "<label>" .$value['filtername']. "</label>";
        echo "</span>";
        echo "</div>"
    } 
}

这篇关于循环遍历2个数组以找出应选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆