循环槽2数组来找出哪些复选框应检查 [英] looping trough 2 array to find out which checkboxes should be checked

查看:72
本文介绍了循环槽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每个值。如果$ adds_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.

我想实现这一目标有以下code:

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)

我想,这里需要不同的方法,但我卡在code几个小时,我正在运行的想法。任何帮助会深深AP preciated。

I guess that different approach is needed here, but i am stuck in the code couple of hours and i am running out of ideas. Any help will be deeply appreciated.

问候,约翰·

推荐答案

一个建议是,与其在整个 $ adds_filters 阵列每次循环,存储所有的'filterid 为一个变量,只是检查 $值['filterid'] 数组里面存在。

下面的code使用功能 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天全站免登陆