thecodedeveloper.com
thecodedeveloper.com

Reputation: 3240

Merge two arrays and selected checkbox in PHP

First Array Output:

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
)   

Second array Output:

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

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

)

My Below code showing all checkbox base on first array

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

if second array category's key value match with first array value so i want to selected checkbox

Upvotes: 1

Views: 326

Answers (2)

ponciste
ponciste

Reputation: 2229

thing is you want to search an element in a multi dimensional array, so you must use a function for this. Try the code below

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

and then

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

This works. I personally tried it.

Upvotes: 0

krishna
krishna

Reputation: 4089

since in_array() will not work in multidimensional array you have to use two foreach loop. so try this

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

Upvotes: 1

Related Questions