在PHP中合并两个数组和选中的复选框

时间:2014-02-19 07:49:12

标签: php arrays

第一个阵列输出:

print_r($categories);
Array
(
    [1] => Accounting & Financial
    [2] => Advertising Services
    [3] => Awards & Incentives
    [4] => Business Consultants
    [5] => Career Services
    [6] => Creative Services
    [7] => Data Management
    [8] => Distributors & Agents
)   

第二个数组输出:

print_r($Service_Provider_Id['Category']);
Array
(
    [0] => Array
        (
            [id] => 1
            [category] => Accounting & Financial
        )

    [1] => Array
        (
            [id] => 2
            [category] => Advertising Services
        )

)

我的下面的代码显示了基于第一个数组的所有复选框

<?phpforeach ($categories as $key => $value) { ?>
                        <div class="checkboxes-div">
                            <input type="checkbox" id="CategoryCategory<?php echo $key; ?>" value="<?php echo $key?>"  name="data[Category][Category][]">
                            <label class="selected" for="CategoryCategory<?php echo $key; ?>">
                            <?php echo $value; ?>
                            </label>
                        </div>
<?php  } ?>

如果第二个数组类别的键值与第一个数组值匹配,那么我想选中复选框

2 个答案:

答案 0 :(得分:1)

由于in_array()在多维数组中不起作用,因此必须使用两个foreach循环。所以试试这个

    <?php
$categories=Array
(
    "1" => "Accounting & Financial",
    "2" => "Advertising Services",
    "3" => "Awards & Incentives",
    "4" => "Business Consultants",
    "5" => "Career Services",
    "6" => "Creative Services",
    "7" => "Data Management",
    "8" => "Distributors & Agents"
) ;

$Service_Provider_Id['Category'] = Array
(
    "0" => Array
        (
            "id" => "1" ,
            "category" => "Accounting & Financial"
        ),

    "1" => Array
        (
            "id" => "2",
            "category" => "Advertising Services"
        )

);

?>



<?php foreach ($categories as $key => $value) { ?>

                        <div class="checkboxes-div">
                            <input type="checkbox" id="CategoryCategory<?php echo $key; ?>" value="<?php echo $key?>"  name="data[Category][Category][]" 
<?php foreach ($Service_Provider_Id['Category'] as $keys => $values) { foreach ($values as $keys2 => $values2) { if(in_array($value,$Service_Provider_Id['Category'][$keys])) {  ?> checked  <?php  } } } ?>  >
                            <label class="selected" for="CategoryCategory<?php echo $value; ?>">
                            <?php echo $value; ?>
                            </label>
                        </div>
<?php  } ?>

答案 1 :(得分:0)

你想要搜索多维数组中的元素,所以你必须使用一个函数。请尝试以下代码

function ArraySearchRecursive($Needle, $Haystack, $NeedleKey = "", $Strict = false, $Path = array()) {
    if (!is_array($Haystack))
        return false;
    foreach ($Haystack as $Key => $Val) {
        if (is_array($Val) &&
                $SubPath = ArraySearchRecursive($Needle, $Val, $NeedleKey, $Strict, $Path)) {
            $Path = array_merge($Path, Array($Key), $SubPath);
            return $Path;
        } elseif ((!$Strict && $Val == $Needle &&
                $Key == (strlen($NeedleKey) > 0 ? $NeedleKey : $Key)) ||
                ($Strict && $Val === $Needle &&
                $Key == (strlen($NeedleKey) > 0 ? $NeedleKey : $Key))) {
            $Path[] = $Key;
            return $Path;
        }
    }
    return false;
}

然后

<?php foreach ($categories as $key => $value) { ?>
    <div class="checkboxes-div">
        <input type="checkbox" id="CategoryCategory<?php echo $key; ?>" value="<?php echo $key; ?>" <?php if (ArraySearchRecursive($value, $Service_Provider_Id['Category'])) { ?> checked <?php } ?> name="data[Category][Category][]">
        <label class="selected" for="CategoryCategory<?php echo $key; ?>">
            <?php echo $value; ?>
        </label>
    </div>
<?php } ?>

这很有效。我亲自尝试过。