title: PHP Removing empty items from object
body: <p>I have the following object, here printed as an array. This object is built from a SOAP request.</p>
  
  <pre><code>AdminInfo Object (
    [Supplier] =&gt; Supplier Object (
        [Party] =&gt; Party Object (
            [OrgInfo] =&gt; OrgInfo Object (
                [CompanyName] =&gt; Bobs
                [IDInfo] =&gt; Array of IDInfo Objects (
                    [0] =&gt; IDInfo Object (
                        [IDQualifierCode] =&gt; 
                        [IDNum] =&gt; 
                      )
                    [1] =&gt; IDInfo Object (
                        [IDQualifierCode] =&gt; CompanyID
                        [IDNum] =&gt; 83e26599-d40g-4cba-9791-7d7c83de282c
                      )
                    [2] =&gt; IDInfo Object (
                        [IDQualifierCode] =&gt; TID
                        [IDNum] =&gt; BOBTID01020304
                      )
                    [3] =&gt; IDInfo Object (
                        [IDQualifierCode] =&gt; Token
                        [IDNum] =&gt; c784570e-044d-42c8-98fe-af9f7c1747f5
                      )
                  )
              )
            [ContactInfo] =&gt; ContactInfo Object (
                [ContactJobTitle] =&gt; 
                [Communications] =&gt; Comm Object (
                    [CommQualifier] =&gt; 
                    [CommPhone] =&gt; 
                    [CommEmail] =&gt; 
                    [Address] =&gt; Address Object (
                        [Address1] =&gt; 
                        [Address2] =&gt; 
                        [City] =&gt; 
                        [StateProvince] =&gt; 
                        [PostalCode] =&gt; 
                        [CountryCode] =&gt; 
                      )
                  )
                [ContactName] =&gt; PersonName Object (
                    [FirstName] =&gt; 
                    [MiddleName] =&gt; 
                    [LastName] =&gt; 
                  )
              )
          )
      )
    [Company] =&gt; Company Object (
        [Party] =&gt; Party Object (
            [OrgInfo] =&gt; OrgInfo Object (
                [CompanyName] =&gt; SF
                [IDInfo] =&gt; 
              )
            [ContactInfo] =&gt; 
          )
      )
    [Facility] =&gt; Facility Object (
        [Party] =&gt; Party Object (
            [OrgInfo] =&gt; Array of OrgInfo Objects (
              )
            [ContactInfo] =&gt; ContactInfo Object (
                [ContactJobTitle] =&gt; Owner
                [Communications] =&gt; Array of Comm Objects(
                    [0] =&gt; Comm Object (
                          [CommQualifier] =&gt; WP
                          [CommPhone] =&gt; 1234567890
                      )
                    [1] =&gt; Comm Object (
                          [CommQualifier] =&gt; SA
                          [Address] =&gt; Address Object (
                              [Address1] =&gt; 123 NE 14th St 
                              [City] =&gt; Nowhere
                              [StateProvince] =&gt; ND
                              [PostalCode] =&gt; 12345
                              [CountryCode] =&gt; US
                            )
                        )
                    [2] =&gt; 
                    [3] =&gt; 
                  )
                [ContactName] =&gt; PersonName Object (
                    [FirstName] =&gt; Bob
                    [MiddleName] =&gt; 
                    [LastName] =&gt; Tester
                  )
              )
          )
      )
  )
  </code></pre>
  
  <p>What I want to do is to remove all the empty elements and be returned with this object</p>
  
  <pre><code>AdminInfo Object (
    [Supplier] =&gt; Supplier Object (
        [Party] =&gt; Party Object (
            [OrgInfo] =&gt; OrgInfo Object (
                [CompanyName] =&gt; Bobs
                [IDInfo] =&gt; Array of IDInfo Objects (
                    [0] =&gt; IDInfo Object (
                        [IDQualifierCode] =&gt; 
                        [IDNum] =&gt; 
                      )
                    [1] =&gt; IDInfo Object (
                        [IDQualifierCode] =&gt; CompanyID
                        [IDNum] =&gt; 83e26599-d40g-4cba-9791-7d7c83de282c
                      )
                    [2] =&gt; IDInfo Object (
                        [IDQualifierCode] =&gt; TID
                        [IDNum] =&gt; BOBTID01020304
                      )
                    [3] =&gt; IDInfo Object (
                        [IDQualifierCode] =&gt; Token
                        [IDNum] =&gt; c784570e-044d-42c8-98fe-af9f7c1747f5
                      )
                  )
              )
          )
      )
    [Company] =&gt; Company Object (
        [Party] =&gt; Party Object (
            [OrgInfo] =&gt; OrgInfo Object (
                [CompanyName] =&gt; SF
              )
          )
      )
    [Facility] =&gt; Facility Object (
        [Party] =&gt; Party Object (
            [ContactInfo] =&gt; ContactInfo Object (
                [ContactJobTitle] =&gt; Owner
                [Communications] =&gt; Array of Comm Objects (
                    [0] =&gt; Comm Object (
                          [CommQualifier] =&gt; WP
                          [CommPhone] =&gt; 1234567890
                      )
                    [1] =&gt; Comm Object (
                          [CommQualifier] =&gt; SA
                          [Address] =&gt; Address Object (
                              [Address1] =&gt; 123 NE 14th St 
                              [City] =&gt; Nowhere
                              [StateProvince] =&gt; ND
                              [PostalCode] =&gt; 12345
                              [CountryCode] =&gt; US
                            )
                        )
                  )
                [ContactName] =&gt; PersonName Object (
                    [FirstName] =&gt; Bob
                    [LastName] =&gt; Tester
                  )
              )
          )
      )
  )
  </code></pre>
  
  <p>These attempts don't do it AT ALL; variable <code>$AdminInfo</code> is the Object above...</p>
  
  <p>From solution here: <a href="https://stackoverflow.com/questions/7741415/strip-null-values-of-json-object">strip null values of json object</a></p>
  
  <pre><code>$json = json_encode($AdminInfo);
  $result = preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $json);
  $echo $result;
  </code></pre>
  
  <p>From solution here: <a href="https://stackoverflow.com/questions/20676771/how-to-remove-null-values-from-an-array">How to remove null values from an array?</a></p>
  
  <pre><code>$json = json_encode($AdminInfo);                // convert to JSON
  $arr = (array)json_decode($json);               // convert to an array
  $object = (object) array_filter((array) $arr);  // filter the array
  $result = json_encode($object);                 // convert it back to JSON
  echo $result;
  </code></pre>
  
  <p>From here: <a href="https://stackoverflow.com/questions/7696548/php-how-to-remove-empty-entries-of-an-array-recursively">PHP - How to remove empty entries of an array recursively?</a></p>
  
  <pre><code>function array_remove_empty($haystack)
  {
      foreach ($haystack as $key =&gt; $value) {
          if (is_array($value)) {
              $haystack[$key] = array_remove_empty($haystack[$key]);
          }
  
          if (empty($value)) {
              unset($haystack[$key]);
          }
      }
  
      return $haystack;
  }
  
  $json = json_encode($AdminInfo);          // convert to JSON
  $arr = (array)json_decode($json);         // convert to an array
  print_r(array_remove_empty($arr));        // run through array_remove_empty function and print
  </code></pre>
  
  <p>From solution here: <a href="https://stackoverflow.com/questions/39138754/remove-items-from-multidimensional-array-in-php">Remove items from multidimensional array in PHP</a></p>
  
  <pre><code>function cleanArray($array) {
      if (is_array($array)) {
          foreach ($array as $key =&gt; $sub_array)
          {
              $result = cleanArray($sub_array);
              if ($result === false) {
                  unset($array[$key]);
              } else {
                  $array[$key] = $result;
              }
          }
      }
      if (empty($array)) {
          return false;
      }
      return $array;
  }
  
  $json = json_encode($AdminInfo);          // convert to JSON
  $arr = (array)json_decode($json);         // convert to an array
  print_r(cleanArray($arr));                // run through cleanArray function and print
  </code></pre>
  
  <p><strong>Edit</strong><br>
  AdminInfo object as JSON:</p>
  
  <pre><code>{
      "Supplier":{
          "Party":{
              "OrgInfo":{
                  "CompanyName":"Bobs",
                  "IDInfo":[
                      {
                          "IDQualifierCode":null,
                          "IDNum":""
                      },
                      {
                          "IDQualifierCode":"CompanyID",
                          "IDNum":"83e26599-d40g-4cba-9791-7d7c83de282c"
                      },
                      {
                          "IDQualifierCode":"TID",
                          "IDNum":"BOBTID01020304"
                      },
                      {
                          "IDQualifierCode":"Token",
                          "IDNum":"c784570e-044d-42c8-98fe-af9f7c1747f5"
                      }
                  ]
              },
              "ContactInfo":{
                  "ContactJobTitle":"",
                  "Communications":{
                      "CommQualifier":null,
                      "CommPhone":"",
                      "CommEmail":"",
                      "Address":{
                          "Address1":"",
                          "Address2":"",
                          "City":"",
                          "StateProvince":null,
                          "PostalCode":"",
                          "CountryCode":null
                      }
                  },
                  "ContactName":{
                      "FirstName":"",
                      "MiddleName":"",
                      "LastName":""
                  }
              }
          }
      },
      "Company":{
          "Party":{
              "OrgInfo":{
                  "CompanyName":"SF",
                  "IDInfo":null
              },
              "ContactInfo":null
          }
      },
      "Facility":{
          "Party":{
              "OrgInfo":[
  
              ],
              "ContactInfo":{
                  "ContactJobTitle":"",
                  "Communications":[
                      {
                          "CommQualifier":null,
                          "CommPhone":"",
                          "CommEmail":"",
                          "Address":{
                              "Address1":"",
                              "Address2":"",
                              "City":"",
                              "StateProvince":null,
                              "PostalCode":"",
                              "CountryCode":null
                          }
                      },
                      null,
                      null,
                      null
                  ],
                  "ContactName":{
                      "FirstName":"Bob",
                      "MiddleName":"",
                      "LastName":"Tester"
                  }
              }
          }
      }
  }
  </code></pre>