How to distinguish between two multidimensional arrays in PHP?

I'm coding an edit form and I can't get the part where you check previously checked checkboxes to function properly.

For example I have a list of five items in a database, I previously checked two of them and saved the form. When I press edit on the form, I need to get this again:

[ ] Item 1
[X] Item 2
[ ] Item 3
[X] Item 4
[ ] Item 5

I have two arrays:

  • $amenities All items in the database are here.

  • $related All previously checked items are here. (in this case, two of them)

How can I walk the two arrays comparing them so if an item in $related is found in $amenities, it will print the box checked and if not, it will print it unchecked. This is my code relevant to that part.

$amenities (print_r)

  Array
(
    [0] => Array
        (
            [itemID] => 3
            [itemName] => Crema Chantilly
        )

    [1] => Array
        (
            [itemID] => 4
            [itemName] => Caribe Cooler
        )

    [2] => Array
        (
            [itemID] => 5
            [itemName] => Cacahuates Japoneses
        )

    [3] => Array
        (
            [itemID] => 6
            [itemName] => Cerveza Sol (lata)
        )

    [4] => Array
        (
            [itemID] => 7
            [itemName] => Chocolate derretido
        )

)

$related (print_r)

  Array
(
    [0] => Array
        (
            [itemID] => 3
            [itemName] => Crema Chantilly
        )

    [1] => Array
        (
            [itemID] => 4
            [itemName] => Caribe Cooler
        )

)

As you can see, there are two items in $related that match two items in $amenities.

What I'm trying here is something like this:

<?php foreach ($related as $single) : ?>

    <?php foreach ($amenities as $amenity) : ?>

      <?php if ( $single === $amenity) : ?>

        <input type="checkbox" class="left" checked="yes" name="amenities[]" value="<?=$amenity['itemID']?>">
        <label class="checkbox"><?=$amenity['itemName']?></label>

       <?php else : ?>

        <input type="checkbox" class="left" name="amenities[]" value="<?=$amenity['itemID']?>">
        <label class="checkbox"><?=$amenity['itemName']?></label>

      <?php endif ?>

    <?php endforeach;?>

<?php endforeach;?>

But, the results are being duplicated for the full options list. I'm getting this:

[X] Item 1
[ ] Item 2
[ ] Item 3
[ ] Item 4
[ ] Item 5
[ ] Item 1
[ ] Item 2
[X] Item 3
[ ] Item 4
[ ] Item 5

And I need to get

[X] Item 1
[ ] Item 2
[X] Item 3
[ ] Item 4
[ ] Item 5

Maybe it's something easy to do but I can't figure out how to get this done. Maybe I am taking the wrong approach with the two foreach?

Answers


Instead of writing in every loop check its existance and set its status checked

<?php foreach ($amenities as $single) : ?>
<?php $strChecked = '';?>
    <?php foreach ($related as $amenity) : ?>

      <?php if ( $single === $amenity) : ?>
          <?php $strChecked = ' checked="checked"';break;?>

      <?php endif ?>

    <?php endforeach;?>

        <input type="checkbox" class="left" <?php echo $strChecked;?> name="amenities[]" value="<?=$amenity['itemID']?>">
        <label class="checkbox"><?=$amenity['itemName']?></label>
<?php endforeach;?>

You can actually do this with a single foreach loop if you use in_array

<?php
    foreach ($amenities as $amenity) {
        $id = $amenity['itemID'];
        $name = $amenity['itemName'];
        $checked = in_array($amenity, $related) ? ' checked="checked"' : '';
        echo <<<HTML
        <input type="checkbox" class="left"$checked name="amenities[]" value="$id">
        <label class="checkbox">$name</label>
        <br>
HTML;
    }
?>

As you can see I also stored the values of $amenity['itemID'] and $amenity['itemName'] in variable to make the HTML code easier to read.

You could also use a slightly more complicated but more flexible approach:

function filter_array_to_ids ($a) {
    return $a['itemID'];
}

// Get an array that contains the id of each item in the
// $related array
$checked_ids = array_map(filter_array_to_ids, $related);

foreach ($amenities as $amenity) {
    $id = $amenity['itemID'];
    $name = $amenity['itemName'];
    $checked = in_array($id, $checked_ids) ? ' checked="checked"' : '';
    echo 'checked = ' . $checked . '<br>';
    echo 'amenity = ' ; print_r($amenity); echo '<br>';
    echo <<<HTML
    <input type="checkbox" class="left"$checked name="amenities[]" value="$id">
    <label class="checkbox">$name</label>
    <br>
HTML;
}

This code uses array_map to create an array that contains only the itemIDs of items in the $related array and then searches for the current amenity's id in that array. This will allow you to find matching ids even if item in $amenities and the item in $related are not exactly the same (if one has extra members, etc).


Need Your Help

compiling java from command prompt and its issues

java command-line-interface

i'm very new to java and only started working two days ago... i decided to work it through command line rather than IDE..

How can I restrict the orientation in a cordova android app based on the device?

cordova

When I use mobile device the orientation should be Portrait.