php - 循环中 array_merge 导致的慢功能
问题描述
我有两个似乎正在减慢 PHP 的函数,我相信这与在循环中使用 array_merge 有关,只是我在努力弄清楚如何才能不这样做。
function fuseOzone($field) {
global $ozone;
if(isset($ozone['all'])) {
foreach($ozone['all'] as $priority => $functions) {
if(isset($ozone[$field][$priority])) {
$ozone[$field][$priority] = array_merge($ozone['all'][$priority], $ozone[$field][$priority]);
} else {
$ozone[$field][$priority] = array_merge($ozone['all'][$priority], array());
$ozone[$field][$priority] = array_unique($ozone[$field][$priority]);
}
}
}
if(isset($ozone[$field])) {
ksort($ozone[$field]);
}
}
function applyOzone($field, $content) {
global $ozone;
fuseOzone($field);
$args = array_slice(func_get_args(), 2);
if(isset($ozone[$field])) {
foreach($ozone[$field] as $priority => $functions) {
if(!is_null($functions)) {
foreach($functions as $function) {
if(!function_exists($function['function'])) {
continue;
}
$all_args = array_merge(array($content), $args);
$function_name = $function['function'];
$accepted_args = $function['accepted_args'];
if($accepted_args === 1) {
$the_args = array($content);
} elseif($accepted_args > 1) {
$the_args = array_slice($all_args, 0, $accepted_args);
} elseif($accepted_args === 0) {
$the_args = null;
} else {
$the_args = $all_args;
}
$content = call_user_func_array($function_name, $the_args);
}
}
}
}
return $content;
}
这些函数是一个更大的类的一部分,它本质上与 Wordpress 操作很相似。即我可以通过过滤或类似的方式连接到一部分内容。
解决方案
推荐阅读
- javascript - 你如何在 React 中渲染 HOC?
- java - Google Drive REST Api 通过其 fileId 下载文件
- .htaccess - 如何在我的主页的搜索控制台中解决 301 重定向
- azure - 使用 Azure 存储创建外部表
- windows - 解释从网站复制的文本字符及其格式
- java - 在 Java 中克隆二维数组
- c# - 无法在 WindowsForm 继承上调整 UI 组件的大小 (Visual Studio 2015)
- ruby-on-rails - 如何在 JS 框中显示带有 Active Storage 的图像变体?
- javascript - Lightbox/iframe 中的 Worldpay Javascript API 托管集成不起作用
- ios - 在 NSMutableAttributedString 的子类中调用 append 时应用程序崩溃