Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_unique() without sorting

Tags:

arrays

php

unique

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?

like image 393
GordonM Avatar asked Mar 18 '11 09:03

GordonM


People also ask

How can I get unique values from two arrays in PHP?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.

How to get unique values in array in PHP?

The array_unique() is a built-in function in PHP and this function removes duplicate values from an array. If there are multiple elements in the array with same values then the first appearing element will be kept and all other occurrences of this element will be removed from the array.

How to remove repeated values in array PHP?

The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.


1 Answers

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

like image 104
Mark Baker Avatar answered Oct 11 '22 07:10

Mark Baker