array_unique() 不排序
我正在构建一个扩展 include_path 的自动加载器。它需要一个数组,附加explode()d包含路径,删除对当前目录的所有引用,在数组的开头添加一个当前目录,最后用join()将整个目录重新组合在一起以形成一个新的包含路径。下面列出了代码,
<?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);
}
?>
我还想在内爆数组之前对数组使用 array_unique() ,这样如果有人粗心并且多次指定相同的路径,PHP 就不会多次查找同一个位置。但是,我还需要维护数组的排序顺序,因为 include 按照包含路径中定义的顺序在目录中查找。我想首先查看当前目录,然后是搜索目录列表,最后是原始包含路径,这样,例如,默认 include_path 中的公共库的旧版本不会包含在较新版本中在我的搜索列表中。
由于这些原因,我无法使用 array_unique() 因为它对数组的内容进行排序。
有没有办法让 array_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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不直接使用array_unique();但 array_unique 确实保留了键,因此您可以随后执行 ksort() 来重新创建条目的原始顺序
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
您也可以使用 array_count_value。您将在函数结果的关键处获得唯一的数组结果,
它不会破坏数组排序内容。
参考: http://php.net/manual/en/function.array -count-values.php
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
像这样的东西:
Something like: