How to find the structure of a Drupal array

Drupal is rife with arrays, so knowing how to work with them effectively will open up some great possibilities for you. There's a lot you can do with arrays, but before you really dig in, here's a short article on how to find the contents of an array.

In Drupal, arrays are handled almost exclusively through vanilla PHP functions. Let's take a look at a common use of an array in Drupal - altering a form using hook_form_alter(). This example comes from the Advanced Poll module:

function advpoll_form_alter(&$form, $form_state, $form_id) {

The first value passed to this function is $form, which is an array containing all the elements in the form. The easiest way to browse the contents of this array is to use a PHP debugger, but we can also use a nice little function called var_dump() to output the array in a fairly readable format. Here's how you would use var_dump():


This will output a tree-like structure containing the values of the $form variable. Here's an example of what a partial var_dump() output looks like:

  'file_directory_path' => 
      '#type' => string 'textfield' (length=9)
      '#title' => string 'File system path' (length=16)
      '#default_value' => string 'sites/default/files' (length=19)
      '#maxlength' => int 255
      '#description' => string 'A file system path where the files will be stored. This directory must exist and be writable by Drupal. If the download method is set to public, this directory must be relative to the Drupal installation directory and be accessible over the web. If the download method is set to private, this directory should not be accessible over the web. Changing this location will modify all download paths and may cause unexpected problems on an existing site.' (length=450)
      '#after_build' => 
          0 => string 'system_check_directory' (length=22)

Items in the tree that aren't contained in quotes usually indicate the type of variable. In this case, the top-most variable is an array, and the array contains other arrays, some strings and an 'int' (integer) value. The next line indicates a key / value pair by linking the key 'file_directory_path' with the value - also an array - through the '=>' symbol. You can see that the output is indented to indicate when one variable is actually inside another variable. If you look down the output, you can see that there is one more array embedded in the embedded array. Arrays like this are called 'nested arrays', and arrays can be nested like this indefinitely. That makes them ideal candidates for organizing hierarchical data.

When you use var_dump() to output a array, you will likely need to view the source code in your browser to view the indentations. You can use var_dump() on any variable, and so it can also be very useful for getting the structure of an object.

In future posts, we'll cover some techniques for manipulating an array.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, its easy to become a member

Add to cart:

Contact us

Below are the supported keyboard shortcuts for our video player.

Start / stop video
Go to next video
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
Jump to a percentage (2 is 20%, for example)
Toggle full screen