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

在PHP中合并两个数组并选中复选框,php,arrays,Php,Arrays,第一个阵列输出: print_r($categories); Array ( [1] => Accounting & Financial [2] => Advertising Services [3] => Awards & Incentives [4] => Business Consultants [5] => Career Services [6] => Creative Services

第一个阵列输出:

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
        )

)
第二个阵列输出:

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  } ?>

由于数组()中的
无法在多维数组中工作,因此必须使用两个
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  } ?>


问题是你们想在多维数组中搜索一个元素,所以你们必须为此使用一个函数。请尝试下面的代码

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 } ?>


您正在使用循环中的第二个数组关于第一个数组?已编辑,请尝试这种方式。要在多维数组中搜索的元素,因此不能使用简单的foreach