循环数组取值效率问题

WBOY
Release: 2016-06-06 20:40:12
Original
1159 people have browsed it

如图所示 三维数组
循环数组取值效率问题

好的。。那么。问题来了。。
如果想删除所有第三层里的seeAnalysis这个键值
正常是用两个foreach,到第三层然后unset
可是就有效率问题
第三层数组可能有数千个之多
循环数组取值效率问题

那末。。有什么提高效率的办法吗~

回复内容:

如图所示 三维数组
循环数组取值效率问题

好的。。那么。问题来了。。
如果想删除所有第三层里的seeAnalysis这个键值
正常是用两个foreach,到第三层然后unset
可是就有效率问题
第三层数组可能有数千个之多
循环数组取值效率问题

那末。。有什么提高效率的办法吗~

为何不换一种思路呢?
你拿着数组是为了什么?还不是为了模板展现,
你在模板展现的时候,是不是需要遍历?
那么,你在模板遍历的时候,先判断seeAnalysis,根据条件忽略不就行了?

<code>php</code><code>bool array_walk_recursive ( array &$input , callable $funcname [, mixed $userdata = NULL ] )
</code>
Copy after login

看一个例子:

<code>php</code><code><?php /** 
 * http://uk1.php.net/array_walk_recursive implementation that is used to remove nodes from the array. 
 * 
 * @param array The input array. 
 * @param callable $callback Function must return boolean value indicating whether to remove the node. 
 * @return array 
 */ 
function walk_recursive_remove (array $array, callable $callback) { 
    foreach ($array as $k => $v) { 
        if (is_array($v)) { 
            $array[$k] = walk_recursive_remove($v, $callback); 
        } else { 
            if ($callback($v, $k)) { 
                unset($array[$k]); 
            } 
        } 
    } 

    return $array; 
} 
?> 
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!