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

时间:2018-08-30 10:50:54

标签: php arrays loops multidimensional-array foreach

我需要做的是在某些条件下在某些数组中添加[checked] => 1: 我有一个变量,想将其与'name'的值进行比较。例如:如果我的变量与“显示”或“转发”匹配,则必须插入新的键和值。

有人可以告诉我该怎么做吗?我想直接转到特定索引(例如0或1)或功能数组的任何索引,并想要插入新的键和值,例如[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 个答案:

答案 0 :(得分:1)

也许您可以使用foreach,检查键/索引在数组中是否存在,并检查“名称”的值。如果匹配,则添加[checked] => 1:

$index = 1;
foreach ($arrays as &$array) {
    if (array_key_exists($index, $array["features"]) &&
        ($array["features"][$index]["name"] === "Display" || $array["features"][$index]["name"] === "Forwarding")
    ) {
        $array["features"][$index]["checked"] = 1;
    }
}

Demo

答案 1 :(得分:1)

您将需要访问name键所在的最低子数组。第一级键是Call xyzHidden。第二级子数组都用features键。第三级具有索引键。 name是第四级的关联密钥。

一旦工作到第四级,就可以根据情况简洁地编写in_array(),以检查最低的数据行是否有资格接收新的checkbox => 1元素。 (或者,您可以省略in_array()调用并使用:$set3["name"] == "Display" || $set3["name"] == "Forwarding",但是扩展起来比较冗长和难看。)

代码:(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);

输出:

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 :(得分:-1)

这很简单。既然您知道需要更改哪个阵列键$index

$array['Call xyz']['features'][$index]['checked'] = 1;