Skip to main content

To sort through a multidimensional array seems to be a function that I occasionally call on.  Handy to have referenced here:

  /**
   * Loop through a multi dimensional array
   * 
   * @param array $haystack
   * Data array.
   */
  protected function sortMultiDimensionalArray($haystack)
  {
    foreach ($haystack as $key => $item) {
      // If $item is an array and the number of children is greater to one
      // keep looping through the array.
      if (is_array($item) && count($haystack[$key]) > 1) {
        $this->sortMultiDimensionalArray($haystack[$key]);
      }
      // If count is equal to one then load it in to the get_value array.
      elseif (is_array($item) && count($haystack[$key]) == 1) {
        $k = key($item);
        if (is_array($item[$k])) {
          $k2 = key($item[$k]);
          $this->get_value[] = $item[$k][$k2];
        }
        else {
          $this->get_value[] = $item[$k];
        }
      }
      // Not an array.
      elseif (!is_array($item)) {
        $this->get_value[] = $item;
      }
    }
  }

 

Related articles

Andrew Fletcher11 Feb 2025
Webpack build process and theme automation improvements
The Drupal theme configuration has undergone recent changes made to the Webpack configuration, SCSS and JavaScript handling, and automation of updates to the orw.libraries.yml file in the custom Drupal theme. These changes are designed to improve the build process, enhance maintainability, and...
Andrew Fletcher22 Jan 2025
Removing a missing module in Drupal
Occasionally, a Drupal site may display a warning about a module being "Missing or invalid." This issue occurs when a module is marked as installed in the configuration but is no longer present in the file system. A common example is the `fakeobjects` module, which is associated with CKEditor 4 and...