I have an array, which holds values like this:
$items_pool = Array (
[0] => Array ( [id] => 1 [quantity] => 1 )
[1] => Array ( [id] => 2 [quantity] => 1 )
[2] => Array ( [id] => 72 [quantity] => 6 )
[3] => Array ( [id] => 4 [quantity] => 1 )
[4] => Array ( [id] => 5 [quantity] => 1 )
[5] => Array ( [id] => 7 [quantity] => 1 )
[6] => Array ( [id] => 8 [quantity] => 1 )
[7] => Array ( [id] => 9 [quantity] => 1 )
[8] => Array ( [id] => 19 [quantity] => 1 )
[9] => Array ( [id] => 20 [quantity] => 1 )
[10] => Array ( [id] => 22 [quantity] => 1 )
[11] => Array ( [id] => 29 [quantity] => 0 )
)
Next, I have a form that I am trying to populate. It loops through the item database, prints out all the possible items, and checks the ones that are already present in $items_pool.
<?php foreach ($items['items_poolpackage']->result() as $item): ?>
<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>">
<?php endforeach; ?>
I know what logically I'm trying to accomplish here, but I can't figure out the programming.
What I'm looking for, written loosely is something like this (not real code):
<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>" <?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>>
Specifically this conditional loop through the array, through all the key values (the ID) and if there's a match, the checkbox is selected.
<?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>
I understand from a loop structured like this that it may mean a lot of 'extra' processing.
TL;DR - I need to loop within the array, check for the key 'id', then print a string.
If I understand correctly, you need something like this?
Conside this though, if you stored your products in a single dimensional array (presuming the ids will always be unique
You will not end up with having duplicated entries of products and can incremement/decrement the quantity easier.
Both techniques should work if the id exists in the array or not.
If you are hoping to store other attritubes in this array other than quantity, you can