gpt4 book ai didi

php - array_unique() 没有排序

转载 作者:可可西里 更新时间:2023-11-01 12:31:36 30 4
gpt4 key购买 nike

我正在构建一个扩展 include_path 的自动加载器。它需要一个数组,追加 explode() 包含路径,删除所有对当前目录的引用,在数组的开头添加一个当前目录,最后 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 中的公共(public)库的旧版本不会被包含以支持新版本在我的搜索列表中。

由于这些原因,我不能使用 array_unique(),因为它对数组的内容进行排序。

有没有办法让 array_unique 保留数组中元素的顺序?

最佳答案

不直接使用array_unique();但是 array_unique 确实保留了键,因此您可以在之后执行 ksort() 以重新创建条目的原始顺序

关于php - array_unique() 没有排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5350080/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com