Can you dig it. Drupal nested array that is.

Spade Large

I sometime need to check arrays for their values dispersed around. Just dumping the array is OK when interested in a particular key or value. But what about those big info arrays from hook_token_info, hook_menu etc. This is what I did.

// dumper.php

function arrayToProperties($values) {
  $is_array = is_array($values);
  if (!$is_array) {
    return $values;
  }
  $result = array();
  while ($is_array) {
    $is_array = FALSE;
    foreach ($values as $key => $data) {
      if (is_array($data)) {
        foreach ($data as $sub_key => $sub_data) {
          $fold = _array_fold($sub_data);
          if (is_array($fold)) {
            foreach ($fold as $k => $val) {
              $result[$key . '.' . $sub_key . '.' . $k] = $val;
            }
          }
          else {
            $result[$key . '.' . $sub_key] = $fold;
          }
        }
      }
      else {
        $result[$key] = $data;
      }
    }
  }
  return $result;
}

This function was developed for https://drupal.org/project/drush_entity. It does a recursive concatenation of nested keys flattening the array into long key-value.

$tree = array(
  'a' => array(
    'b' => 'value b',
    'c' => array(
      'd' => 'value d',
    ),
  ),
);

$lines = arrayToProperties($tree));

This results into an array structure like

Array
(
    [a.b] => value b
    [a.c.d] => value d
)

This is still not output to use with ie diff so we need some more work. We want to print both the key and value.

// dumper.php cont.

function printArrayAsProperties($info) {
  $lines = arrayToProperties($info);
  array_walk($lines, function(&$item, $key) {
    $item = "$key = $item";
  });
  echo join("\n", $lines) . "\n";
}

Now if your want to know some drupal array structure try this

// dumper.php cont.

//list($info) = menu_router_build();
$info = token_info();

printArrayAsProperties($info);

Next run it against a drupal site with drush or use devel dpm or dsm.

drush @drupal.d7 php-script dumper.php

This results into
types.comment.name = Comments
types.comment.description = Tokens for comments posted on the site.
types.comment.needs-data = comment
types.node.name = Nodes
types.node.description = Tokens related to individual content items, or "nodes".
types.node.needs-data = node
types.site.name = Site information
types.site.description = Tokens for site-wide settings and other global information.
types.date.name = Dates
types.date.description = Tokens related to times and dates.
types.file.name = Files
types.file.description = Tokens related to uploaded files.
types.file.needs-data = file
...
tokens.user.url.description = The URL of the account profile page.
tokens.user.edit-url.name = Edit URL
tokens.user.edit-url.description = The URL of the account edit page.
tokens.user.last-login.name = Last login
tokens.user.last-login.description = The date the user last logged in to the site.
tokens.user.last-login.type = date
tokens.user.created.name = Created
tokens.user.created.description = The date the user account was created.
tokens.user.created.type = date

Now we can dig some tokens :)
drush @drupal.d7 php-script dumper.php | grep "^tokens\.user"