9

I'm building an autoloader that extends the include_path. It takes an array, appends the explode()d include path, removes all references to the current directory, adds a single current directory at the start of the array, and finally join() the whole thing back together to form a new include path. The code is listed below

<?php
static public function extendIncludePath (array $paths)
{
    // Build a list of the current and new paths
    $pathList   = array_merge (explode (PATH_SEPARATOR, $paths), explode (PATH_SEPARATOR, get_include_path ()));
    // Remove any references to the current directory from the path list
    while ($key = array_search ('.', $pathList))
    {
        unset ($pathList [$key]);
    }
    // Put a current directory reference to the front of the path
    array_unshift ($pathList, '.');
    // Generate the new path list
    $newPath    = implode (PATH_SEPARATOR, $pathList);
    if ($oldPath = set_include_path ($newPath))
    {
        self::$oldPaths []  = $oldPath;
    }
    return ($oldPath);
}
?>

I wanted to also use array_unique() on the array before imploding it so that PHP doesn't keep looking in the same place multiple times if someone is careless and specifies the same path more than once. However, I also need to maintain the sort order of the array because include looks in the directories in the order they're defined in the include path. I want to look in the current directory first, then my list of search directories and finally the original include path so that, for example, an old version of a common library in the default include_path doesn't get included in favour of a newer version in my search list.

For these reasons I can't use array_unique() because it sorts the array's contents.

Is there a way of getting array_unique to preserve the order of the elements in my array?

1
  • 1
    Sort of bypassing your enquiry: Is $array = array_flip(array_flip($array)) an option for you? (array_flip swaps values with keys, and since keys are always unique, a double array_flip makes the values in your array unique.) Commented Mar 18, 2011 at 9:27

3 Answers 3

8

Not using array_unique() directly; but array_unique does preserve the keys, so you can do a ksort() afterwards to recreate the original order of entries

Sign up to request clarification or add additional context in comments.

Comments

5

you may also use array_count_value. you will get your unique array result at key of your function result

its doesn't break your array sort content.

reference : http://php.net/manual/en/function.array-count-values.php

1 Comment

Ooh, creative approach! Quickref for the curious: $unique = array_keys(array_count_values($source));
1

Something like:

$temp = array();

foreach ( $original_array as $value ) {
    $temp[$value] = 1;
}

$original_array = array_keys($temp);

3 Comments

Heads-up: There's a function for that loop [effectively; if you don't care for the value being 1]. array_flip(). :)
Oh damn I fell to PHP rule #0: there's always a PHP function you never heard of :)
Bahaha. :D * sings a song to the PHP functions that lurk in wait to ambush the unsuspecting, praying for safe passage! *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.