[英]How to get the difference between two multidimensional arrays of associative arrays in PHP?
[英]How to distinguish between two multidimensional arrays in PHP?
我正在编写一个编辑表单,但我无法获得您将先前选中的复选框正确选中到 function 的部分。
例如,我在数据库中有一个包含五个项目的列表,我之前检查了其中两个并保存了表格。 当我在表单上按编辑时,我需要再次获取:
[ ] Item 1
[X] Item 2
[ ] Item 3
[X] Item 4
[ ] Item 5
我有两个 arrays:
$amenities
数据库中的所有项目都在这里。
$related
所有以前检查过的项目都在这里。 (在这种情况下,其中两个)
我如何走这两个 arrays 比较它们,所以如果在$amenities
中找到$related
中的项目,它将打印选中的框,如果没有,它将打印未选中的框。 这是我与那部分相关的代码。
$便利设施 (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
)
)
$相关(print_r)
Array
(
[0] => Array
(
[itemID] => 3
[itemName] => Crema Chantilly
)
[1] => Array
(
[itemID] => 4
[itemName] => Caribe Cooler
)
)
如您所见, $related
中有两项与$amenities
中的两项相匹配。
我在这里尝试的是这样的:
<?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
[ ] Item 3
[ ] Item 4
[ ] Item 5
[ ] Item 1
[ ] Item 2
[X] Item 3
[ ] Item 4
[ ] Item 5
我需要得到
[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;?>
如果你使用in_array
,你实际上可以用一个foreach
循环来做到这一点
<?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;
}
?>
如您所见,我还将$amenity['itemID']
和$amenity['itemName']
的值存储在变量中,以使 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;
}
此代码使用array_map
创建一个仅包含$related
数组中项目的itemID
的数组,然后在该数组中搜索当前便利设施的 ID。 这将允许您找到匹配的 ID,即使$amenities
中的项目和$related
中的项目不完全相同(如果有额外的成员等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.