简体   繁体   English

如何基于数组设置选定的多个组合框

[英]how to set selected multiple combo box based on array

I have two sets of array ,first array contains all categories called "all", and second array contains selected categories called "selected", I want to populate this concept to multiple combo box, 我有两组数组,第一个数组包含称为“ all”的所有类别,第二个数组包含称为“ selected”的选定类别,我想将此概念填充到多个组合框中,

$all = [
        0 => [
            'id'=>1,
            'name' => 'news'
        ],
        1 => [
            'id'=>2,
            'name' => 'tips'
        ],
        2 => [
            'id'=>3,
            'name' => 'trick'
        ],
        3 => [
            'id'=>4,
            'name' => 'review'
        ]
    ];

    $selected = [
        0 => [
            'id'=>2,
            'name' => 'trick'
        ],
        1 => [
            'id'=>4,
            'name' => 'review'
        ],
    ];

I've try to do foreach in foreach , but i have duplicated data when show in combo box, i want to have all data from "all" shown with selected data from "selected". 我已经尝试在foreach中执行foreach,但是在组合框中显示时我有重复的数据,我希望将“ all”中的所有数据与“ selected”中的选定数据一起显示。

i just solved my problem in deferent way , first i add default pair of key and value "sel"=>0 in "all" array set, then i loop trough array "all" and array "sel" to get similar value and when it match change sel key to 1 ,this code for further explanation 我只是以不同的方式解决了我的问题,首先我在“所有”数组集中添加了默认的键和值对“ sel” => 0,然后循环通过槽“ all”和数组“ sel”以获得相似的值,以及何时它将更改sel键匹配为1,此代码可作进一步说明

public static function compare($sel,$all){
    // add sel key with default value = 0
    foreach($all as $k=>$v){
        $all[$k]['sel'] = 0;
    }

    foreach($all as $k=>$v){
        foreach($sel as $k2=>$v2){
            // when match change sel to 1
            if($v['id'] == $v2['id']){
                $all[$k]['sel'] = 1;
            }

        }
    }
    return $all;
}

final result : 最后结果 :

$all = [
        0 => [
            'id'=>1,
            'name' => 'news',
            'sel' => 0
        ],
        1 => [
            'id'=>2,
            'name' => 'tips',
            'sel' => 0
        ],
        2 => [
            'id'=>3,
            'name' => 'trick',
            'sel' => 1
        ],
        3 => [
            'id'=>4,
            'name' => 'review',
            'sel' => 1
        ]
    ];

just add if condition when $all['sel'] = 1 they should be selected, thanks all :D 仅在条件$ all ['sel'] = 1时添加条件,谢谢:D

You can get the intersection of both arrays with array_uintersect and a custom callback function ( compare ). 您可以使用array_uintersect和自定义的回调函数( compare )获得两个数组的交集。

function compare($a, $b){
    if($a['id'] == $b['id']){
        return 0;
    }
    return 1;
}


$res =  array_uintersect($selected, $all,"compare");
print_r($res);

>Array ( [0] => Array ( [id] => 2 [name] => trick ) [1] => Array ( [id] => 4 [name] => review ) ) 

After that you only need to loop through the final array and set the corresponding check boxes. 之后,您只需要遍历最终数组并设置相应的复选框。

If you want to compare by name just create another callback function. 如果要按名称进行比较,只需创建另一个回调函数。

function compare2($a, $b){
    if($a['name'] == $b['name']){
        return 0;
    }
    return 1;
}

The duplicates are caused by the inner for loop continuing to create select elements even after it has found a selected element. 重复是由内部for循环(即使在找到选定元素之后)继续创建select元素引起的。 You can avoid having an inner loop and using php's in_array() function to check if $all is in $selected like this: 您可以避免内部循环,并使用php的in_array()函数检查$ all是否在$ selected中,如下所示:

$x = '';
foreach($all as $a){
    if(in_array($a, $selected)){
        $x .= '<option selected>'.$a['id'].'Selected </option>';
     }else{
      $x .= '<option>'.$a['id'].'Not selected </option>';
     }
}
echo $x;

Note that in_array will check all values of the elements, so for example element with id 2 but different name will appear as not selected. 请注意,in_array将检查元素的所有值,因此,例如ID为2但名称不同的元素将显示为未选中。 You may want to change both names to tips. 您可能需要将两个名称都更改为提示。 I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于另一个组合框的所选项目填充数据库中组合框的项目 - how to fill the items in combo box from database based on the selected item of another combo box 如何根据ajax的其他选定组合框值填充一个组合框 - How to fill one combo box based an other selected combo box value by ajax 如何在php的组合框中显示选定的值? - How to display selected value in combo box in php? 如何使用PHP在选择框中选择从多个选项或具有不同值的数组到视图的设置选项 - How to set an option from multiple options or array with different values to views as selected in select box using PHP 从数据库中获取值,具体取决于在多个组合框中选择的选项 - Fetching values from databse depending on the options selected in multiple combo box 如何在WPF中为组合框项目设置值? - How to set a value for a combo box item in WPF? 如何在另一个组合框所选值的基础上填充一个组合框 - How to fill one combo box basing other combo box selected value 我如何从组合框中选择项目到codeigniter中的控制器 - How i get selected item from combo box to controller in codeigniter 如何从php的组合框中显示选定的值 - how to display the selected value from combo box in php 如何从组合框中的数据库中获取选定的值 - How to get selected value from database in combo box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM