To::query()

Converts PHP array to URL query string.

Table of Contents
  1. Description
  2. Example
  3. Conversion

Description

To::query(?array $value = []): ?string;

This method converts PHP array to URL query string. Combine this method with From::query() to safely modify URL query string data.

Example

// Parse URL query string to PHP array
$query = From::query('?foo=bar&baz=qux');

// Modify URL query string with PHP array access
$query['baz'] = 1;
$query['qux'] = 2;

// Convert PHP array data back to URL query string
$query = To::query($query); // Returns `?baz=1&foo=bar&qux=2`

Conversion

Below are examples of input data along with the conversion results:

FromTo
nullnull
[]null
['a' => 'b']'?a=b'
['a' => ['b']]'?a[0]=b'
['a' => ['b', 'c']]'?a[0]=b&a[1]=c'
['a' => ['c', 'b']]'?a[0]=c&a[1]=b'
['a' => [1 => 'c', 2 => 'b']]'?a[1]=c&a[2]=b'
['a' => ['b' => 'c']]'?a[b]=c'
['a' => true, 'b' => ""]'?a&b='
['a' => false, 'b' => null, 'c' => true]'?c'
['a' => 'false', 'b' => 'null', 'c' => 'true']'?a=false&b=null&c=true'

To::URL()

Converts private file path to public URL, or decodes URL special characters.

To::entity()

Converts characters to their corresponding HTML entities.

To::file()

Converts current value to a safe file name.

To::path()

Converts public URL to private file path.

To::query()

Converts PHP array to URL query string.