function drush_lookup_field_by_path

8.0.x output.inc drush_lookup_field_by_path($input, $path, $mapping = array(), $default_value = NULL)
6.x output.inc drush_lookup_field_by_path($input, $path, $mapping = array(), $default_value = NULL)
7.x output.inc drush_lookup_field_by_path($input, $path, $mapping = array(), $default_value = NULL)
master output.inc drush_lookup_field_by_path($input, $path, $mapping = array(), $default_value = NULL)

Return a specific item inside an array, as identified by the provided path.

Parameters

$input:: An array of items, potentially multiple layers deep.

$path:: A specifier of array keys, either in an array or separated by a '/', that list the elements of the array to access. This works much like a very simple version of xpath for arrays, with all items being treated identically (like elements).

$mapping:: (optional) An array whose keys may correspond to the $path parameter and whose values are the corresponding paths to be used in $input.

Example 1:

$input = array('#name' => 'site.dev', '#id' => '222'); $path = '#name'; result: 'site.dev';

Example 2:

$input = array('ca' => array('sf' => array('mission'=>array('1700'=>'woodward')))); $path = 'ca/sf/mission/1701'; result: 'woodward'

Example 3:

$input = array('#name' => 'site.dev', '#id' => '222'); $path = 'name'; $mapping = array('name' => '#name'); result: 'site.dev';

Related topics

2 calls to drush_lookup_field_by_path()
drush_rows_of_key_value_to_array_table in includes/output.inc
Convert an array of data rows, where each row contains an associative array of key : value pairs, into a table suitable for processing by drush_print_table. The provided $header determines the order that the items will appear in the output. Only data…
drush_select_output_fields in includes/output.inc
Select the fields from the input array that should be output.

File

includes/output.inc, line 452

Code

function drush_lookup_field_by_path($input, $path, $mapping = array(), $default_value = NULL) {
  $result = '';
  if (isset($mapping[$path])) {
    $path = $mapping[$path];
  }
  if (!is_array($path)) {
    $parts = explode('/', $path);
  }
  if (!empty($parts)) {
    $result = $input;
    foreach ($parts as $key) {
      if ((is_array($result)) && (isset($result[$key]))) {
        $result = $result[$key];
      }
      else {
        return $default_value;
      }
    }
  }
  return $result;
}