ees3
ees3

Reputation: 105

Delete element from triply nested array PHP

I am struggling to figure out how to remove elements from a triple nested array based on a value at the deepest level. I would like to remove any position sub-array where time == "NA". My array structure is as follows.

Array
(
    [0] => Array
        (
            [Id] => 151601
            [First_Name] => JOHN
            [Last_Name] => DOE
            [Location_Id] => 10
            [Positions] => Array
                (
                    [North] => Array
                        (
                            [Current_Level] => 4
                            [Last_Date] => 11/7/2001
                            [Time] => 4:15 AM
                        )

                    [East] => Array
                        (
                            [Current_Level] => 4
                            [Last_Date] => 7/10/2003
                            [Time] => 7:30 PM
                        )

                    [South] => Array
                        (
                            [Current_Level] => 2
                            [Last_Date] => 8/10/2007
                            [Time] => NA
                        )

                    [West] => Array
                        (
                            [Current_Level] => NA
                            [Last_Date] => NA
                            [Time] => NA
                        )

                )

        )

So my end result would be

Array
    (
        [0] => Array
            (
                [Id] => 151601
                [First_Name] => JOHN
                [Last_Name] => DOE
                [Location_Id] => 10
                [Positions] => Array
                    (
                        [North] => Array
                            (
                                [Current_Level] => 4
                                [Last_Date] => 11/7/2001
                                [Time] => 4:15 AM
                            )
    
                        [East] => Array
                            (
                                [Current_Level] => 4
                                [Last_Date] => 7/10/2003
                                [Time] => 7:30 PM
                            )
    
                    )
    
            )

This is what I am currently trying but it is throwing an illegal offset type error. I think I'm just not unsetting the right thing. I can get it to echo all the correct subarrays but when I try to unset I get an offset error.

foreach($records as $record) {
    foreach ($record as $value) {
      if (is_array($value)) {
        foreach ($value as $position) {
          if($position["Time"] == "NA") {
            unset($records[$record][$value]);
          }
        }
      }
    }
  }

Upvotes: 0

Views: 154

Answers (2)

u_mulder
u_mulder

Reputation: 54841

With passing array element by reference and filtering function you can reduce your code to:

foreach($records as &$record) {
    $record['Positions'] = array_filter(
        $record['Positions'], 
        function ($v) {
            return $v['Time'] !== 'NA';
        }
    );
}

Fiddle here.

Upvotes: 2

The fourth bird
The fourth bird

Reputation: 163342

Php uses a copy of the array in the foreach. You might also use a key in the foreach and use that to unset the value in the original $records array.

foreach ($records as $keyRecord => $record) {
    foreach ($record as $key => $value) {
        if (is_array($value)) {
            foreach ($value as $keyPosition => $position) {
                if ($position["Time"] == "NA") {
                    unset($records[$keyRecord][$key][$keyPosition]);
                }
            }
        }
    }
}

print_r($records);

Output

Array
(
    [0] => Array
        (
            [Id] => 151601
            [First_name] => John
            [Positions] => Array
                (
                    [North] => Array
                        (
                            [Current_Level] => 4
                            [Last_Date] => 11/7/2001
                            [Time] => 4:15 AM
                        )

                    [East] => Array
                        (
                            [Current_Level] => 4
                            [Last_Date] => 7/10/2003
                            [Time] => 7:30 PM
                        )

                )

        )

)

Php demo

Upvotes: 1

Related Questions