qazzu
qazzu

Reputation: 411

Reduce the use of foreach in array when checking null values php

I have a multidimensional array like below:

 $data = array (
        'department1' =>array(
            'user1' => array(
                'building1' => array(
                    'room1' => array(
                        'active' =>'false'
                    ),
                    'room2' => array(
                    )
                ),
                'building2' => array(
                    'room4' => array(
                    ),
                    'room3' => array(
                    )
                ),
            )
        )
    );

What I am trying to do is to check the room if is null or not. If null I should set a value active => test. The code I've done so far is:

 foreach($data as $departments => $department){
      foreach ($department as $users => $user){
         foreach($user as $buildings => $building) {
            foreach($building as $key => $value){
                  if ($data[$departments][$users][$buildings][$key] == null) {
                  $data[$departments][$users][$buildings][$key]['active'] = 'test';
                  }
            }

         }
      }
    }

It's working but I want to know if there is a best other way to implement it without used much foreach.

My output is:

 Array
 (
[department1] => Array
    (
        [user1] => Array
            (
                [building1] => Array
                    (
                        [room1] => Array
                            (
                                [active] => false
                            )

                        [room2] => Array
                            (
                                [active] => test
                            )

                    )

                [building2] => Array
                    (
                        [room4] => Array
                            (
                                [active] => test
                            )

                        [room3] => Array
                            (
                                [active] => test
                            )

                    )

            )

    )

) Any help? Thank you.

Upvotes: 4

Views: 270

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Use the following recursive function which is accepting and returning value by reference(can be used with any item deepness/hierarchy):

function &checkRoom(&$data) {
    foreach ($data as $k => &$v) {
        if (strpos($k, "room") === 0 && empty($v)) $v = ["active" => "test"];
        if (is_array($v)) checkRoom($v);
    }
    return $data;
}

$data = checkRoom($data);
print_r($data);

The output:

Array
(
    [department1] => Array
        (
            [user1] => Array
                (
                    [building1] => Array
                        (
                            [room1] => Array
                                (
                                    [active] => false
                                )

                            [room2] => Array
                                (
                                    [active] => test
                                )
                        )

                    [building2] => Array
                        (
                            [room4] => Array
                                (
                                    [active] => test
                                )

                            [room3] => Array
                                (
                                    [active] => test
                                )

                        )
                )
        )
)

Upvotes: 1

Peter
Peter

Reputation: 9123

You can use array_walk_recursive() to walk through your $data array without having to foreach on each level of depth.

array_walk_recursive($data, function (&$value, $key) {
    if (is_null($value) && $key == 'active') {
        $value = 'test';
    }
});

The above code example will walk through your $data array and change each item with key 'active' that has value 'NULL' to value 'test'.

Note that the 'active' key has to be set in order for this to work.

Resources

Upvotes: 1

Related Questions