You are here

function _hierarchical_select_create_code_from_array in Hierarchical Select 7.3

Given a array, create the export code for it.

This functions is a refactoring of features_var_export() to use with the hierarchical select module.

Parameters

mixed $config: A value to export as code.

string $prefix: Padding for nested array.

boolean $init: Indicator of the first level of the export.

Return value

string The code as it would appear in an editor.

1 call to _hierarchical_select_create_code_from_array()
_hierarchical_select_create_export_code in ./hierarchical_select.admin.inc
Given a config array, create the export code for it.

File

./hierarchical_select.admin.inc, line 278
Module settings and configuration administration UI.

Code

function _hierarchical_select_create_code_from_array($var, $prefix = '', $init = TRUE) {
  $output = "";
  $type = gettype($var);
  switch ($type) {
    case 'array':
      if (empty($var)) {
        $output = "array()";
      }
      else {
        $output = "array(\n";
        foreach ($var as $key => $value) {
          $value = _hierarchical_select_create_code_from_array($value, '  ', FALSE);
          $output .= "  '{$key}' => " . $value . ",\n";
        }
        $output .= ')';
      }
      break;
    case 'string':
      $var = str_replace("\n", "***BREAK***", $var);
      $output = var_export($var, TRUE);
      break;
    case 'boolean':
      $var = empty($var) ? 'FALSE' : 'TRUE';
      $output = var_export($var, TRUE);
      break;
    default:
      $output = var_export($var, TRUE);
  }
  if ($prefix) {
    $output = str_replace("\n", "\n{$prefix}", $output);
  }
  if ($init) {
    $output = str_replace("***BREAK***", "\n", $output);
  }
  return $output;
}