CI_URI::uri_to_assoc()

uri_to_assoc([$n = 3[, $default = array()]])

Parameters:
  • $n (int) – Segment index number
  • $default (array) – Default values
Returns:

Associative URI segments array

Return type:

array

This method lets you turn URI segments into an associative array of key/value pairs. Consider this URI:

index.php/user/search/name/joe/location/UK/gender/male

Using this method you can turn the URI into an associative array with this prototype:

[array]
(
        'name'          => 'joe'
        'location'      => 'UK'
        'gender'        => 'male'
)

The first parameter lets you set an offset, which defaults to 3 since your URI will normally contain a controller/method pair in the first and second segments. Example:

$array = $this->uri->uri_to_assoc(3);
echo $array['name'];

The second parameter lets you set default key names, so that the array returned will always contain expected indexes, even if missing from the URI. Example:

$default = array('name', 'gender', 'location', 'type', 'sort');
$array = $this->uri->uri_to_assoc(3, $default);

If the URI does not contain a value in your default, an array index will be set to that name, with a value of NULL.

Lastly, if a corresponding value is not found for a given key (if there is an odd number of URI segments) the value will be set to NULL.

doc_CodeIgniter
2016-10-15 16:32:03
Comments
Leave a Comment

Please login to continue.