0

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]);
          }
        }
      }
    }
  }

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is returning the nested array as is. Nothing is being filtered
Note &$record with & sign. Added a fiddle also.
1

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

Comments

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.