这里我有两个数组1.response 2.selected_amenties,在第一个数组值我显示在复选框中,现在我想为游泳池和电源备份制作检查值,因为此值等于第一个数组(响应),怎么做呢?
<?php
$response = Array
(
Array
(
"id" => "57e2340eaebce1023152759b",
"name" => "Squash Court",
"amenityType" => "Sports"
),
Array
(
"id" => "57e23470aebce1023152759d",
"name" => "Swimming Pool",
"amenityType" => "Sports"
),
Array
(
"id" => "57e2347caebce1023152759e",
"name" => "Power Backup",
"amenityType" => "Convenience"
),
Array
(
"id" => "57e23486aebce1023152759f",
"name" => "Day Care Center",
"amenityType" => "Convenience"
)
);
$selected_amenties = Array( "0" => "Swimming Pool",
"1" => "Power Backup"
);
foreach($response as $amenity)
{
?>
<div class="checkbox">
<input type="checkbox" class="aminit" name="aminit" value="<?php echo $amenity['name']?>"><?php echo $amenity['name']?>
</div>
<?php
}
?>
答案 0 :(得分:1)
试试这样:
<?php
foreach($response as $amenity)
{
$checked = in_array($amenity['name'], $selected_amenties) ? 'checked' : '';
?>
<div class="checkbox">
<input type="checkbox" class="aminit" name="aminit" value="<?php echo $amenity['name'] ?>" <?php echo $checked; ?>><?php echo $amenity['name']?>
</div>
<?php
}
?>
答案 1 :(得分:0)
$selected_amenties = array('Swimming Pool', 'Power Backup');
foreach($response as $amenity) {
$check = '';
in_array($amenity, $selected_amenties) and $check = ' checked="checked" ';
echo '<div class="checkbox">';
echo '<input type="checkbox" ' . $check . ' class="aminit" name="aminit" value="<?php echo $amenity['name']?>"><?php echo $amenity['name']?>';
echo '</div>';
}