1

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

2 Answers 2

1

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  } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

you are just using second array in loop wht about first array?
edited, try this way. the element you want to search in on a multi dimensional array, so you can't do it with a simple foreach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.