dream3323 2018-08-30 10:50
浏览 36
已采纳

在PHP中设置多维数组对象中的键和值

All i need is to add [checked] => 1 in some of the arrays on a condition: i have a variable and wants to compare that with the value of 'name' . Ex: If my variable matches with Display or Forwarding, then insertion of a new key and value has to be done.

can anybody tell me how to do that ? I want to directly go to specific index like at 0 or 1 or any index of features array and want to insert new key and value like [checked] => 1

   Array
        (
            [Call xyz] => Array
                (
                    [features] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Display
                                    [display] => webxxx
                                    [category] => x
                                    [uniq_id] => x
                                )

                            [1] => Array
                                (
                                    [name] => Forwarding
                                    [display] => webxxx
                                    [category] => x
                                    [uniq_id] => x
                                )
                        )
               )
           [Hidden] => Array
               (
                   [features] => Array
                      (
                         [0] => Array
                            (
                                [name] => XYZ
                                [display] => webxxx
                                [category] => x
                                [uniq_id] => x
                        )

                        [1] => Array
                          (
                               [name] => ABC
                               [display] => webxxx
                               [category] => x
                               [uniq_id] => x
                          )
                   )
             )
  • 写回答

3条回答 默认 最新

  • duan1982453 2018-08-30 14:11
    关注

    You will need to access down to the lowest subarray where the name key resides. The first level keys are Call xyz and Hidden. The second level subarrays are both keyed with features. The third level has indexed keys. name is an associative key on the fourth level.

    Once you work your way down to the fourth level, you can concisely write in_array() in your condition to check if the lowest row of data qualifies to receive the new checkbox => 1 element. (Alternatively, you could omit the in_array() call and use: $set3["name"] == "Display" || $set3["name"] == "Forwarding" but this is more verbose and uglier to scale up.)

    Code: (Demo)

    $array = [
        "Call xyz" => [
            "features" => [
                ["name" => "Display", "display" => "webxxx", "category" => "x", "uniq_id" => "x"],
                ["name" => "Forwarding", "display" => "webxxx", "category" => "x", "uniq_id" => "x"]
            ]
        ],
        "Hidden" => [
            "features" => [
                ["name" => "XYZ", "display" => "webxxx", "category" => "x", "uniq_id" => "x"],
                ["name" => "ABC", "display" => "webxxx", "category" => "x", "uniq_id" => "x"]
            ]
        ]
    ];
    
    $whitelist = ["Display", "Forwarding"];
    
    foreach ($array as $key1 => $set1) {
        foreach ($set1 as $key2 => $set2) {
            foreach ($set2 as $key3 => $set3) {
                if (in_array($set3["name"], $whitelist)) {
                    $array[$key1][$key2][$key3]["checkbox"] = 1;    
                }
            }
        }
    }
    
    var_export($array);
    

    Output:

    array (
      'Call xyz' => 
      array (
        'features' => 
        array (
          0 => 
          array (
            'name' => 'Display',
            'display' => 'webxxx',
            'category' => 'x',
            'uniq_id' => 'x',
            'checkbox' => 1,
          ),
          1 => 
          array (
            'name' => 'Forwarding',
            'display' => 'webxxx',
            'category' => 'x',
            'uniq_id' => 'x',
            'checkbox' => 1,
          ),
        ),
      ),
      'Hidden' => 
      array (
        'features' => 
        array (
          0 => 
          array (
            'name' => 'XYZ',
            'display' => 'webxxx',
            'category' => 'x',
            'uniq_id' => 'x',
          ),
          1 => 
          array (
            'name' => 'ABC',
            'display' => 'webxxx',
            'category' => 'x',
            'uniq_id' => 'x',
          ),
        ),
      ),
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵