比较复选框的关联数组键和值
Compare associative array keys and values for checkbox
我有一个带有事件空间复选框的 Web 表单。
复选框值是从我的数据库 "facility_event_charges" 中的一个 table 中获取的:
假设我勾选了 5 和 6,"Audio Visual Equipment" 和 "Audio Visual Technician" 并提交了我的表格。
这会将 ID 保存到一个单独的 table 中,我为此 "Event Space"
假设我的 "Event Space" 的 ID 为 63。查找 table 现在有两个条目用于我的 ID,因为我选中了两个复选框:
一切都很好,得救了。
现在,假设我需要实际编辑与此相关的费用 space。
当我点击编辑时,我的网络表单呈现了表单字段,但复选框是空的!!为什么?因为它正在从我的 "facility_event_charges" table 抓取数据。
我需要将此 table 数据(数组)与保存在我的查找 table(数组)中的数据进行比较,以确定哪些值是共同的,并在这些复选框中进行检查。
在我的编辑中想要的结果 space webform 会检查那些
我的复选框"charges"数组:
array(6) {
[1]=> string(9) "Officials"
[2]=> string(6) "Ushers"
[3]=> string(19) "Additional Staffing"
[4]=> string(16) "Special Lighting"
[5]=> string(22) "Audio Visual Equipment"
[6]=> string(23) "Audio Visual Technician"
}
--
我也可以这样生成费用:
array(6) {
[0]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "1" ["type"]=> string(9) "Officials" } }
[1]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "2" ["type"]=> string(6) "Ushers" } }
[2]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "3" ["type"]=> string(19) "Additional Staffing" } }
[3]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "4" ["type"]=> string(16) "Special Lighting" } }
[4]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "5" ["type"]=> string(22) "Audio Visual Equipment" } }
[5]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "6" ["type"]=> string(23) "Audio Visual Technician" } } }
我的查找 table 数组:
array(2) {
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } }
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } }
}
--
我的尝试一直没有成功 - 尝试过 php array_intersect
array_intersect_assoc
array_map('array_diff_assoc')
我需要遍历复选框费用数组并从查找 table 数组中找到一个匹配项,当它匹配时,标记复选框 "checked"。
谁有我可以测试的工作示例?
复选框费用数组 key 需要匹配 "facility_event_charges_id"
这有效...
但它会根据您最终构建数组的准确程度而改变...但这应该只需要更改 if-then 语句并且可能需要更改 i(索引变量)的值。
<html>
<head></head>
<body>
<form>
<?php
// assume you build this based on a sql query
// you don't need to use the primary key for the
// array index
$charges[0]="Officials";
$charges[1]="Ushers";
$charges[2]="Additional Staffing";
$charges[3]="Special Lighting";
$charges[4]="AV Equipment";
$charges[5]="AV Technician";
// your array of eventcharges
// select facility_event_charges_id from tbl where facility_event_spaces_id='63'
// loop thru the result of teh query make a simple array of your evencharge ids
// gonna hardcode mine out of laziness
// since php is typeless we can use strings to compare to ints
// directly as long as we use == and not ===
// and in_array() (used below) goes with loose comparison too
$ec[]="4";
$ec[]="5";
// now we have an array of our descriptions
// and an array of the already selected choices
// from the db data
for($i=0;$i<count($charges);$i++){
print('<input type="checkbox" name="charges" value="'.$i.'" ');
if(in_array($i,$ec)){
print("checked");
}
print(" > ".$charges[$i]."<br />");
}
?>
</form>
</body></html>
根据上面给出的数组,您可以这样比较
例如让take $charge array as
array(6) {
[1]=> string(9) "Officials"
[2]=> string(6) "Ushers"
[3]=> string(19) "Additional Staffing"
[4]=> string(16) "Special Lighting"
[5]=> string(22) "Audio Visual Equipment"
[6]=> string(23) "Audio Visual Technician"
}
拿$lookup array as
array(2) {
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } }
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } }
}
你可以compare both array like below
$charges = array(
1 => "Officials",
2 => "Ushers",
3 => "Additional Staffing",
4 => "Special Lighting",
5 => "Audio Visual Equipment",
6 => "Audio Visual Technician"
);
$lookup = array(
array("EventCharge" => array(
"facility_event_charges_id" => "5"
)
), array(
"EventCharge" => array(
"facility_event_charges_id" => "6"
)
)
);
$lookup = array_column(array_column($lookup, 'EventCharge'), 'facility_event_charges_id');
foreach ($charges as $key => $value) {
echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $lookup)?"checked":"").'>';
echo '<label>'.$value.'<label>';
}
编辑
如果 array_column() 不受支持,您可以使用如下循环创建数组并测试
$arr=array();
foreach ($lookup as $key => $value) {
foreach ($value as $k => $v) {
$arr[]=$v['facility_event_charges_id'];
}
}
foreach ($charges as $key => $value) {
echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $arr)?"checked":"").'>';
echo '<label>'.$value.'<label>';
}
您好,您可以使用以下 code.i 假设您有 $arr=array('5','6');来自 database.so 你可以按如下方式我们
<?php
$arr=array('5'=>'Audio Visual Equipment','6'=>'Audio Visual Technician');
$arr=array_keys($arr);//if you have associated array
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="ISO-8859-1">
</head>
<body>
<input type="checkbox" name="charges" value="1" <?=in_array('1', $arr)?'checked=true':''?>> Officials<br>
<input type="checkbox" name="charges" value="2" <?=in_array('2', $arr)?'checked=true':''?>> Ushers<br>
<input type="checkbox" name="charges" value="3" <?=in_array('3', $arr)?'checked=true':''?>> Additional Staffing <br>
<input type="checkbox" name="charges" value="4" <?=in_array('4', $arr)?'checked=true':''?>> Special Lighting <br>
<input type="checkbox" name="charges" value="5" <?=in_array('5', $arr)?'checked=true':''?>> Audio Visual Equipment<br>
<input type="checkbox" name="charges" value="6" <?=in_array('6', $arr)?'checked=true':''?>> Audio Visual Technician<br>
</body>
</html>
我有一个带有事件空间复选框的 Web 表单。
复选框值是从我的数据库 "facility_event_charges" 中的一个 table 中获取的:
假设我勾选了 5 和 6,"Audio Visual Equipment" 和 "Audio Visual Technician" 并提交了我的表格。
这会将 ID 保存到一个单独的 table 中,我为此 "Event Space" 假设我的 "Event Space" 的 ID 为 63。查找 table 现在有两个条目用于我的 ID,因为我选中了两个复选框:
一切都很好,得救了。 现在,假设我需要实际编辑与此相关的费用 space。
当我点击编辑时,我的网络表单呈现了表单字段,但复选框是空的!!为什么?因为它正在从我的 "facility_event_charges" table 抓取数据。
我需要将此 table 数据(数组)与保存在我的查找 table(数组)中的数据进行比较,以确定哪些值是共同的,并在这些复选框中进行检查。
在我的编辑中想要的结果 space webform 会检查那些
我的复选框"charges"数组:
array(6) {
[1]=> string(9) "Officials"
[2]=> string(6) "Ushers"
[3]=> string(19) "Additional Staffing"
[4]=> string(16) "Special Lighting"
[5]=> string(22) "Audio Visual Equipment"
[6]=> string(23) "Audio Visual Technician"
}
-- 我也可以这样生成费用:
array(6) {
[0]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "1" ["type"]=> string(9) "Officials" } }
[1]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "2" ["type"]=> string(6) "Ushers" } }
[2]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "3" ["type"]=> string(19) "Additional Staffing" } }
[3]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "4" ["type"]=> string(16) "Special Lighting" } }
[4]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "5" ["type"]=> string(22) "Audio Visual Equipment" } }
[5]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "6" ["type"]=> string(23) "Audio Visual Technician" } } }
我的查找 table 数组:
array(2) {
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } }
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } }
}
-- 我的尝试一直没有成功 - 尝试过 php array_intersect array_intersect_assoc array_map('array_diff_assoc')
我需要遍历复选框费用数组并从查找 table 数组中找到一个匹配项,当它匹配时,标记复选框 "checked"。
谁有我可以测试的工作示例? 复选框费用数组 key 需要匹配 "facility_event_charges_id"
这有效...
但它会根据您最终构建数组的准确程度而改变...但这应该只需要更改 if-then 语句并且可能需要更改 i(索引变量)的值。
<html>
<head></head>
<body>
<form>
<?php
// assume you build this based on a sql query
// you don't need to use the primary key for the
// array index
$charges[0]="Officials";
$charges[1]="Ushers";
$charges[2]="Additional Staffing";
$charges[3]="Special Lighting";
$charges[4]="AV Equipment";
$charges[5]="AV Technician";
// your array of eventcharges
// select facility_event_charges_id from tbl where facility_event_spaces_id='63'
// loop thru the result of teh query make a simple array of your evencharge ids
// gonna hardcode mine out of laziness
// since php is typeless we can use strings to compare to ints
// directly as long as we use == and not ===
// and in_array() (used below) goes with loose comparison too
$ec[]="4";
$ec[]="5";
// now we have an array of our descriptions
// and an array of the already selected choices
// from the db data
for($i=0;$i<count($charges);$i++){
print('<input type="checkbox" name="charges" value="'.$i.'" ');
if(in_array($i,$ec)){
print("checked");
}
print(" > ".$charges[$i]."<br />");
}
?>
</form>
</body></html>
根据上面给出的数组,您可以这样比较
例如让take $charge array as
array(6) {
[1]=> string(9) "Officials"
[2]=> string(6) "Ushers"
[3]=> string(19) "Additional Staffing"
[4]=> string(16) "Special Lighting"
[5]=> string(22) "Audio Visual Equipment"
[6]=> string(23) "Audio Visual Technician"
}
拿$lookup array as
array(2) {
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } }
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } }
}
你可以compare both array like below
$charges = array(
1 => "Officials",
2 => "Ushers",
3 => "Additional Staffing",
4 => "Special Lighting",
5 => "Audio Visual Equipment",
6 => "Audio Visual Technician"
);
$lookup = array(
array("EventCharge" => array(
"facility_event_charges_id" => "5"
)
), array(
"EventCharge" => array(
"facility_event_charges_id" => "6"
)
)
);
$lookup = array_column(array_column($lookup, 'EventCharge'), 'facility_event_charges_id');
foreach ($charges as $key => $value) {
echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $lookup)?"checked":"").'>';
echo '<label>'.$value.'<label>';
}
编辑
如果 array_column() 不受支持,您可以使用如下循环创建数组并测试
$arr=array();
foreach ($lookup as $key => $value) {
foreach ($value as $k => $v) {
$arr[]=$v['facility_event_charges_id'];
}
}
foreach ($charges as $key => $value) {
echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $arr)?"checked":"").'>';
echo '<label>'.$value.'<label>';
}
您好,您可以使用以下 code.i 假设您有 $arr=array('5','6');来自 database.so 你可以按如下方式我们
<?php
$arr=array('5'=>'Audio Visual Equipment','6'=>'Audio Visual Technician');
$arr=array_keys($arr);//if you have associated array
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="ISO-8859-1">
</head>
<body>
<input type="checkbox" name="charges" value="1" <?=in_array('1', $arr)?'checked=true':''?>> Officials<br>
<input type="checkbox" name="charges" value="2" <?=in_array('2', $arr)?'checked=true':''?>> Ushers<br>
<input type="checkbox" name="charges" value="3" <?=in_array('3', $arr)?'checked=true':''?>> Additional Staffing <br>
<input type="checkbox" name="charges" value="4" <?=in_array('4', $arr)?'checked=true':''?>> Special Lighting <br>
<input type="checkbox" name="charges" value="5" <?=in_array('5', $arr)?'checked=true':''?>> Audio Visual Equipment<br>
<input type="checkbox" name="charges" value="6" <?=in_array('6', $arr)?'checked=true':''?>> Audio Visual Technician<br>
</body>
</html>