You are here

function query_parameters_to_url_encode_query_parameter_values in Query Parameters To URL 7

Encodes an array of nested query parameters into a clean string.

Example:

  • an equivalent array of ?a[0][1][2]=3&b[4][5]=6&c=7 will become
  • /p/a/0__1__2__3/b/4__5__6/c/7
2 calls to query_parameters_to_url_encode_query_parameter_values()
QueryParametersToURLUnitTestCase::testQueryParameterValuesEncoding in ./query_parameters_to_url.test
Tests if a query parameter's values are properly encoded.
query_parameters_to_url_url_outbound_alter in ./query_parameters_to_url.module
Implements hook_url_outbound_alter().

File

./query_parameters_to_url.module, line 271
Query Arguments To URL module.

Code

function query_parameters_to_url_encode_query_parameter_values($value, $parents = '', $depth = 0) {
  $nested_key = query_parameters_to_url_nested_key();
  $nested_value = query_parameters_to_url_nested_values_delimiter();
  $encoded = '';
  if (is_array($value)) {

    // Recursively goes through the array, prefixing the value with the key
    // parents.
    foreach ($value as $key => $element) {
      $next_parent = $parents . $key . $nested_key;
      $encoded .= query_parameters_to_url_encode_query_parameter_values($element, $next_parent, $depth + 1);
    }
  }
  elseif (!empty($value)) {
    $encoded .= $parents;
    $encoded .= $value;
    if (!empty($parents)) {
      $encoded .= $nested_value;
    }
  }

  // Trim the last delimiter.
  if ($depth == 0) {
    $encoded = rtrim($encoded, $nested_value);
  }
  return $encoded;
}