0

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
                          )
                   )
             )
1
  • In the future, please include your best coding attempt in your question. This helps us to know that you have tried to self-solve, that you'd done some research, and we may only need to make minor adjustments to your work (as opposed to writing a full snippet for you). Commented Aug 31, 2018 at 6:17

3 Answers 3

2

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',
      ),
    ),
  ),
)
Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps you could use a foreach, check if the key/index exists on the array and check the values for "name". If that matches add [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

Comments

-2

This is pretty simple. Given that you know which array key $index you need to change :

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

3 Comments

wait but its not just call xyz, we have multiple arrays like this
Again, if you have that key, just replace it with a variable like i did $index
something like $array[$call]['features'][$index]['checked'] = 1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.