0

我有以下 PHP 代码:

$age = array_merge($age,array(get_user_meta($comment->user_id, 'wsl_user_age', true)));

这会产生:

Array
(
    [0] => 26
    [1] => 
    [2] => 18
    [3] => 28
    [4] => 22
    [5] => 21
    [6] => 26
    [7] => 
    [8] => 
    [9] => 
    [10] => 
    [11] => 
    [12] =>
    [13] =>
    [14] =>
    [15] =>
    [16] =>
    [17] =>
    [18] =>
    [19] =>
    [20] =>
)

我想要的是 26 18 等作为键。并重视它重复的次数,本质上是重复计数。

我试过:

$age = array_merge($age,array(get_user_meta($comment->user_id, 'wsl_user_age', true)=> ""))

但这产生了绝对的废话。

我应该尝试什么?

4

3 回答 3

0

我想你正在寻找array_count_values

$cnt_array = array_count_values($your_array);

echo "<pre>";
print_r($cnt_array);

参考:http ://www.php.net/manual/en/function.array-count-values.php

于 2013-03-15T11:40:17.947 回答
0

这是您可能正在寻找的东西http://codepad.org/RDUhltQ7

看一看

于 2013-03-15T11:52:04.530 回答
0
$foo = Array
(   26,
    18,
    28,
    22,
    21,
    26,
    '',
    ''
);

$foo = array_filter($foo);
$foo = array_count_values($foo);

print_r($foo);

http://sandbox.onlinephpfunctions.com/code/a1875256f4680b06b9d909bdb3329cfcd64d44de

于 2013-03-15T11:52:37.400 回答