【问题标题】:Compare associative array keys and values for checkbox比较复选框的关联数组键和值
【发布时间】:2016-10-06 18:07:24
【问题描述】:

我有一个带有 Event Spaces 复选框的 Web 表单。

复选框值是从我的数据库“facility_event_charges”中的一个表中获取的:

假设我检查了 5 和 6,“视听设备”和“视听技术员”并提交了我的表单。

这会将 ID 保存到我为该“事件空间”拥有的单独表中 假设我的“事件空间”的 ID 为 63。由于我选中了 2 个复选框,因此查找表现在有两个条目作为我的 ID:

一切都很好并且得救了。 现在,假设我需要实际编辑与该空间相关的费用。

当我单击编辑时,我的网络表单呈现表单字段但复选框为空!!为什么?因为它正在从我的“facility_event_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" } } } 

我的查找表数组:

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')

我需要遍历复选框费用数组并从查找表数组中找到匹配项,当匹配时,将复选框标记为“已选中”。

谁有我可以测试的工作示例? 复选框费用数组 key 是匹配 "facility_event_charges_id"

所需要的

【问题讨论】:

    标签: php checkbox cakephp-1.3


    【解决方案1】:

    根据你上面给出的数组,你可以像这样比较

    例如让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>';
    }
    

    【讨论】:

    • 我会尝试,但 array_column 需要 php 5.5。收到致命错误:调用未定义函数 array_column()
    • 谢谢!我已经接受了这个经过编辑的答案并获得了赏金。非常感谢。
    【解决方案2】:

    这行得通……

    但它会根据您最终构建数组的准确程度而改变......但这应该只需要更改 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>
    

    【讨论】:

    • 我正在尝试这个,但是在让我的数组变成 $ec[] $ec is 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"] => 字符串(1) "6" } } }
    • 发布您如何构建包含(最终)您的活动费用的阵列。可能是一种更简单的方法,因此它将是一个非常简单的数组,例如我的 $ec
    • $sqlFields = array("facility_event_charges_id"); $sqlConditions = array("facility_event_spaces_id"=>$id); $sqlOrderBy = array("facility_event_spaces_id asc"); $sqlParams = array('fields'=>$sqlFields,'conditions'=>$sqlConditions,'order'=>$sqlOrderBy); $ec = $this->EventCharge->find('all',$sqlParams); $this->set('ec',$ec);
    • 按空间 id 查找所有内容 - 在本例中 id = 63
    • 导致数组(2){[0]=>数组(1){[“EventCharge”]=>数组(1){[“facility_event_charges_id”]=>字符串(1)“ 5" } } [1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } } }
    【解决方案3】:

    您好,您可以使用以下代码。我假设您有 $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>
    

    【讨论】:

      猜你喜欢
      • 2013-07-03
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 1970-01-01
      相关资源
      最近更新 更多