Reputation: 693
I have 2 multidimensional arrays $investmentProgramExistingCriteriaoutput
and $criteria
which when printed (print_r) produces the following output:
Array $investmentProgramExistingCriteriaoutput:
Array
(
[0] => Array
(
[key1] => 1
[key2] => 4
[criteriaID] => 25
)
[1] => Array
(
[key1] => 2
[key2] => 4
[criteriaID] => 26
)
)
Array $criteria output:
Array
(
[0] => Array
(
[criteriaID] => 27
[key3] => 1
[key4] => Some value
[key5] => Yes
[key6] => 3
[key7] => 1
)
[1] => Array
(
[criteriaID] => 25
[key3] => 5
[key4] => Some other value
[key5] => 1, 2, 3
[key6] => 1
[key7] => 1
)
[2] => Array
(
[criteriaID] => 26
[key3] => 1
[key4] => Some different value
[key5] => Ναί
[key6] => 1
[key7] => 1
)
)
I am trying to generate check boxes for each item in $criteria
array. However, if the criteriaID
in the $criteria
array exists in the $investmentProgramExistingCriteriaoutput
array, the checkbox should be checked, if not, it should be unchecked.
I am trying to do that with the following code:
foreach ($criteria as $val) {
foreach ($investmentProgramExistingCriteria as $existingcriteria) {
if($val['criteriaID'] == $existingcriteria['criteriaID']) {
echo "<input type='checkbox' name='criteria[]' value=".$val['criteriaID']." style='margin-bottom:20px;float:left;' checked='checked' /> ";
echo "<span style='line-height:20px;'>".$val['criteriaDescription'] ."</span><br /><br />";
} else {
echo "<input type='checkbox' name='criteria[]' value=".$val['criteriaID']." style='margin-bottom:20px;float:left;' /> ";
echo "<span style='line-height:20px;'>".$val['criteriaDescription'] ."</span><br /><br />";
}
}
}
As you can see from the above array values, I should get 3 checkboxes, from which 2 should be checked. However this code print 6 checkboxes instead of 3,
2 of the 6 printed checkboxes them are checked (correctly). How can I get rid of duplicate checkboxes?
Upvotes: 1
Views: 44
Reputation: 94662
You are of course getting both conditions in your inner loop as you process through the array
Instead build an array from $investmentProgramExistingCriteria
containing just the field you are interested in, and use in_array()
to do the test just once per outter loop
$existingcriteria = array_column($investmentProgramExistingCriteria, 'criteriaID');
foreach ($criteria as $val) {
if ( in_array($val['criteriaID'], $existingcriteria ) {
echo "<input type='checkbox' name='criteria[]' value=".$val['criteriaID']." style='margin-bottom:20px;float:left;' checked='checked' /> ";
echo "<span style='line-height:20px;'>".$val['criteriaDescription'] ."</span><br /><br />";
} else {
echo "<input type='checkbox' name='criteria[]' value=".$val['criteriaID']." style='margin-bottom:20px;float:left;' /> ";
echo "<span style='line-height:20px;'>".$val['criteriaDescription'] ."</span><br /><br />";
}
}
Upvotes: 1
Reputation: 33813
Rather than nested loops you might try iterating through the first array ( renamed here for brevity ) and assigning criteriaID
into a new array. Then iterate through the criteria
array and see if the value exists.
$ipec=array(
array('k1'=>1,'k2'=>4,'criteriaID'=>25),
array('k1'=>2,'k2'=>4,'criteriaID'=>26)
);
$criteria=array(
array('criteriaID'=>27,'k3'=>1,'k4'=>4),
array('criteriaID'=>25,'k3'=>1,'k4'=>4),
array('criteriaID'=>26,'k3'=>1,'k4'=>4)
);
$cids=array();
foreach($ipec as $a)$cids[]=$a['criteriaID'];
foreach($criteria as $a){
$c=$a['criteriaID'];
$checked = in_array( $c, $cids ) ? 'checked=true' : '';
/* There is no key "criteriaDescription" in the quoted arrays */
echo "
<input type='checkbox' name='criteria[]' value='{$c}' style='margin-bottom:20px;float:left;' {$checked} />
<span style='line-height:20px;'>{$val['criteriaDescription']}</span>
<br />
<br />";
}
Upvotes: 2