CodeIgniter Forums
uri_to_multiassoc() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: uri_to_multiassoc() (/thread-31189.html)



uri_to_multiassoc() - El Forum - 06-09-2010

[eluser]GSV Sleeper Service[/eluser]
this may be useful to someone, very similar to uri_to_assoc(), but allows for multiple values.
stick this is in your libraries/MY_URI.php
Code:
function uri_to_multiassoc($offset) {
    $return_array = array();
    $url_array  = array_slice($this->segment_array(),$offset);        
    for($a=0; $a < count($url_array); $a=$a+2){
        $return_array[$url_array[$a]][] = $url_array[$a+1];
    }        
    return $return_array;
}
example URI
/controller/method/reptiles/gecko/reptiles/salamander/fish/trout/reptiles/monitor_lizard/fish/salmon
example print_r($this->uri->uri_to_multiassoc(2));
Code:
Array
(
    [reptiles] => Array
        (
            [0] => gecko
            [1] => salamander
            [2] => monitor_lizard
        )

    [fish] => Array
        (
            [0] => trout
            [1] => salmon
        )

)