仅打印来自两个关联数组的匹配值

时间:2014-05-29 11:41:55

标签: php arrays associative-array

我有这两个关联数组。

enter image description here

我想打印匹配的category_idCHECKED,其余Un-CHECKED

这是我尝试过的。

foreach ($all_available_categories as $category) {
     if(in_array($category['category_id'], $current_item_categories)){
           echo 'CHECKED';
     }
  echo '<input type="checkbox" name="item_categories_checkbox[]" value="'   .$category['category_id'] . '">' . $category['category_name'] . "</br>";
}

5 个答案:

答案 0 :(得分:0)

好吧,您可以先创建一个当前项目类别的关联数组,只需按类别ID键入即可快速查找。

<?php 

$currentCatIds = array();
foreach ($current_item_categories as $category) {
    $currentCatIds[$category['category_id']] = 1;
} 

foreach ($all_available_categories as $category) {
    $catid = $category['category_id'];
    // Forgot this one
    $checked = isset($currentCatIds[$catId]) ? 'checked' : '';
    echo '<input ' . $checked . ' type="checkbox" name="item_categories_checkbox[]" value="' . $catid . '" />' . $category['category_name'] . '<br />';        
}

答案 1 :(得分:0)

你应该做这样的事情:

步骤1:修改你的$ current_item_categories数组,如下所示:

$currentCategories = array();
foreach($current_item_categories as $currCat) {
    array_push($currentCategories, $currCat['category_id']);
}

步骤2:现在我使用的是与您应用的相同的逻辑,只需稍加修改。

foreach ($all_available_categories as $category) {
     if(in_array($category['category_id'], $currentCategories)){
          $checked = 'checked="checked"';
     }else {
        $checked = "";
     }
  echo '<input ' . $checked . 'type="checkbox" name="item_categories_checkbox[]" value="'   .$category['category_id'] . '">' . $category['category_name'] . "</br>";
}

注意:代码未经过测试。

答案 2 :(得分:0)

foreach ($all_available_categories as $category) {
     foreach ($next_available_categories as $category1) {
          if($category['category_id'] == $category1['category_id']){
            echo 'CHECKED'; break;
          }
     }
     echo '<input type="checkbox" name="item_categories_checkbox[]" value="'   .$category['category_id'] . '">' . $category['category_name'] . "</br>";
}

答案 3 :(得分:0)

这个想法非常简单:循环所有类别,然后为每个类别检查是否选中此类别。您正在尝试使用PHP函数in_array()来执行后者,但该函数不会进行多维搜索。因此,只有在您拥有类别ID的数组$ids = array('9', '13');时才会有效,然后使用in_array()检查类别ID。

因此,如果我们进行这些修改,它就会起作用:

$ids = array();
foreach($current_item_categories as $category) {
    $ids[] = $category['category_id'];
}

foreach ($all_available_categories as $category) {
    if(in_array($category['category_id'], $ids)){
        echo 'CHECKED';
    }
    echo '<input type="checkbox" name="item_categories_checkbox[]" value="' . $category['category_id'] . '">' . $category['category_name'] . '</br>';
}

另一种方法是使用两个嵌套循环。这将不再需要一系列id,并允许进行其他检查:

foreach ($all_available_categories as $category) {
    foreach ($current_item_categories as $c) {
        if ($category['category_id'] == $c['category_id']) {
            echo 'CHECKED';
        }
    }

    echo '<input type="checkbox" name="item_categories_checkbox[]" value="' . $category['category_id'] . '">' . $category['category_name'] . '</br>';
}

最后,你应该意识到你的代码没有检查复选框,它只打印单词&#34; CHECKED&#34;。如果这不是你想要的,你应该在输入元素中添加checked属性。

答案 4 :(得分:0)

感谢Frog的回答。

这是完全正常工作的代码。

 $current_ids = array();
             foreach ($current_item_categories as $category) {
                    $current_ids[] = $category['category_id'];
                }

                foreach ($all_available_categories as $category) {
                    echo '<input type="checkbox" name="item_categories_checkbox[]" value="' . $category['category_id'] . '"';
                    if (in_array($category['category_id'], $current_ids)) {
                        echo ' CHECKED ';
                    }
                    echo ' >' . $category['category_name'] . '</br>';
                }