PHP多维数组中统计元素个数

WBOY
Release: 2016-06-06 20:17:42
Original
1935 people have browsed it

<code>Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 12
                            [name] => '1'
                        )

                    [1] => Array
                        (
                            [id] => 28
                            [name] => '2'
                        )
                        .....
                )
            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 121
                            [name] => '2'
                        )

                    [1] => Array
                        (
                            [id] => 281
                            [name] => '4'
                        )
                        ...
                )
        )
        ....
 )</code>
Copy after login
Copy after login

我想统计name对应的值出现的次数,
比如name='2'出现的次数是2
比如name='4'出现的次数是1

有什么好的办法么?

只能使用遍历的话,如何效率最高?

可以使用外部工具:mysql,Memcache、redis。

回复内容:

<code>Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 12
                            [name] => '1'
                        )

                    [1] => Array
                        (
                            [id] => 28
                            [name] => '2'
                        )
                        .....
                )
            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 121
                            [name] => '2'
                        )

                    [1] => Array
                        (
                            [id] => 281
                            [name] => '4'
                        )
                        ...
                )
        )
        ....
 )</code>
Copy after login
Copy after login

我想统计name对应的值出现的次数,
比如name='2'出现的次数是2
比如name='4'出现的次数是1

有什么好的办法么?

只能使用遍历的话,如何效率最高?

可以使用外部工具:mysql,Memcache、redis。

<code class="php">
$result = array();

array_walk_recursive($demo, function($value,$key) use(&$result){
     if ($key == 'name') {
         $result[$value] += 1;
     }
});

print_r($result);</code>
Copy after login

不知道能不能满足你的需求。最终的数组中的key==name的值

首先,比较中意楼上的方法
其次,这个本身也可以通过递归函数的方式来实现

<code>function doFind(array $demo, &$result) {
    foreach ($demo as $key => $value) {
        if (is_array($value)) {
            doFind($value, $result);
        }
        if ($key == 'name') {
            if (!isset($result[$value])) {
                $result[$value] = 1;
            } else {
                $result[$value]++;
            }
        }
    }
}

$ret = [];
doFind($demo, $ret);
var_dump($ret);</code>
Copy after login
<code>通过对数组进行递归判断,进行值统计。</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