Using WordPress ‘rawurlencode_deep()’ PHP function

The rawurlencode_deep WordPress PHP function navigates through an array, object, or scalar, and raw-encodes the values to be used in a URL.

Usage

rawurlencode_deep( $value );

Example:

Input:

$data = ['apple', 'orange', 'banana'];
$encoded_data = rawurlencode_deep($data);

Output:

$encoded_data = ['apple', 'orange', 'banana'];

Parameters

  • $value (mixed) – The array, object, or scalar to be encoded.

More information

See WordPress Developer Resources: rawurlencode_deep

Examples

Encoding a simple array

In this example, we are encoding a simple array with fruit names.

$data = ['apple', 'orange', 'banana'];
$encoded_data = rawurlencode_deep($data);

Encoding an associative array

In this example, we are encoding an associative array with fruit names and their colors.

$data = [
  'apple' => 'green',
  'orange' => 'orange',
  'banana' => 'yellow'
];
$encoded_data = rawurlencode_deep($data);

Encoding a nested array

In this example, we are encoding a nested array containing fruit names and their colors.

$data = [
  'fruits' => [
    'apple' => 'green',
    'orange' => 'orange',
    'banana' => 'yellow'
  ]
];
$encoded_data = rawurlencode_deep($data);

Encoding a string with special characters

In this example, we are encoding a string containing special characters that need to be URL-encoded.

$string = 'Hello, World! How are you?';
$encoded_string = rawurlencode_deep($string);

Encoding an object

In this example, we are encoding an object containing fruit names and their colors.

class Fruit {
  public $name;
  public $color;

  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
}

$apple = new Fruit('apple', 'green');
$encoded_apple = rawurlencode_deep($apple);