如何在PHP中区分两个多维数组?
我正在编写一个编辑表单,我无法获取您之前选中复选框的部分以正常工作 例如,我在数据库中有一个包含五个项目的列表,我之前检查了其中两个项目并保存了表单。当我在表单上按edit时,我需要再次获取以下信息:如何在PHP中区分两个多维数组?,php,arrays,loops,checkbox,Php,Arrays,Loops,Checkbox,我正在编写一个编辑表单,我无法获取您之前选中复选框的部分以正常工作 例如,我在数据库中有一个包含五个项目的列表,我之前检查了其中两个项目并保存了表单。当我在表单上按edit时,我需要再次获取以下信息: [ ] Item 1 [X] Item 2 [ ] Item 3 [X] Item 4 [ ] Item 5 我有两个阵列: $professionals数据库中的所有项目都在这里 $related所有以前选中的项目都在这里。(在这种情况下,其中两个) 我如何浏览两个数组,比较它们,以便如果
[ ] Item 1
[X] Item 2
[ ] Item 3
[X] Item 4
[ ] Item 5
我有两个阵列:
数据库中的所有项目都在这里$professionals
所有以前选中的项目都在这里。(在这种情况下,其中两个)$related
$related
中找到$professionals
中的项目,它将打印选中的框,如果没有选中,它将打印未选中的框。这是我与该部分相关的代码
$便利设施(印刷版)
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
中有两个项目与$professionals
中的两个项目相匹配
我在这里尝试的是这样的:
<?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;?>
我需要得到
[X] Item 1
[ ] Item 2
[X] Item 3
[ ] Item 4
[ ] Item 5
也许这很容易做到,但我不知道如何才能做到。可能我对两个
foreach
?采取了错误的方法,而不是在每个循环中写入检查其存在并将其状态设置为选中
<?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;?>
不要在每个循环中写入,而是检查其存在性并将其状态设置为选中
<?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;?>
如果您使用
此代码用于创建一个数组,该数组仅包含
$related
数组中项目的itemID
s,然后在该数组中搜索当前设施的id。这将允许您找到匹配的ID,即使$professionals
中的项目与$related
中的项目不完全相同(如果有额外的成员等)。如果使用
此代码用于创建一个数组,该数组仅包含
$related
数组中项目的itemID
s,然后在该数组中搜索当前设施的id。这将允许您找到匹配的ID,即使$professionals
中的项目与$related
中的项目不完全相同(如果有额外的成员等)。感谢您的快速回答,但是,我也需要显示未选中的选项。通过您提供的代码,我确实检查了两个相关选项,但我仍然需要显示未检查的选项,正如我在原始问题的“和我需要获取”部分中所述。感谢您的快速回答,但是,我也需要显示未检查的选项。通过您提供的代码,我确实检查了两个相关选项,但我仍然需要显示未检查的选项,正如我在原始问题的“和我需要获取”部分中所述。只是为了让您知道使用更方便,谢谢您的建议。我使用CodeIgniter,它可以选择动态转换为,所以我认为只要我继续使用CI,它就不会成为问题。只是想让你知道,使用更方便,谢谢你的建议。我使用CodeIgniter,它可以选择动态转换为,所以我认为只要我继续使用CI,就不会有问题。
<?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;?>
<?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;
}
?>
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;
}