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.


hi how generate a number of row

Great tutorials for us beginners and thanks a lot.
by the way i would like to know how to set a number of row.

Some thing like :

i have a field in wich i enter a number 5.

On submit , it will add five more row .


Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's 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
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
1 month
6 months
10% off
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.