CodeIgniter Forums

Full Version: URI Library (v1.6.1) - small optimization suggestion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]coolfactor[/eluser]
The _reindex_segments() function is a tad long in the tooth for what it does.

Here is the current version:
Code:
function _reindex_segments()
{
    $i = 1;

    foreach ($this->segments as $val)
    {
        $this->segments[$i++] = $val;
    }

    unset($this->segments[0]);
        
    $i = 1;
        
    foreach ($this->rsegments as $val)
    {
        $this->rsegments[$i++] = $val;
    }
        
    unset($this->rsegments[0]);
}

The same thing can be accomplished in fewer lines using native PHP functions. No need for the overhead of looping:
Code:
function _reindex_segments() {

    array_unshift($this->segments, NULL);
    unset($this->segments[0]);

    array_unshift($this->rsegments, NULL);
    unset($this->rsegments[0];

}

El Forum

[eluser]FlashUK[/eluser]
Great suggestion.

array_unshift is php4 friendly too so I don't see why this shouldn't be used instead.

Just to point out: Don't forget to close the bracket on the second unset (you might want to edit your post to correct this)

P.S: This can be found in the file system/libraries/URI.php