Search code examples
phparraysmultidimensional-arrayunset

How to delete a nested array inside of an array


I have an multidimensional array made out of form data that can look like this example:

array [
  "absender" => "Maxim Ivan",
  "email" => "maximivan@example.com",
  "telefon" => "1234567890",
  "fax" => null,
  "grund" => "Gehaltserhöhung",
  "termin" => [
     0 => [
       "person" => "Some Name",
       "meeting" => "10.05"  
     ],
     1 => [
       "person" => "Another Name",
       "meeting" => "18.05"  
     ],
     2 => [
       "person" => "Again another name",
       "meeting" => null,
       "next-possible-meeting" => "1"  
     ],
     3 => [
       "person" => "And again",
       "meeting" => null,
       "next-possible-meeting" => "1"  
     ],
      4 => [
        "meeting" => null,
     ],
  "bemerkung" => "some notes by Maxim"
]

'person' and 'next-possible-meeting' are checkboxes while 'meeting' is a textarea. I only need the 'termin'-data when 'person' exists, so I store each of them in different arrays (one for persons, one for meetings and one for next-possible-meetings).

That means I don't need the array to be nested anymore, the 'termin' can (and should) be deleted out of it. But I don't know how to access the nested array correctly.

How do I delete the whole 'termin'-array out of the whole array and therefore make it a normal and not a multidimensional array?


Solution

  • Use unset()

    if(isset($array['termin'])){
      unset ($array['termin']);
    }
    

    Output:- https://3v4l.org/1Yj4F

    Note:- isset() used to check that index exist or not, if not function call will saved.