开发者

Count number of different strings?

I have an array that looks like

Array
(
    [1] => Array
        (
            [0] => Date
            [1] => Action
        )

    [2] => Array
        (
            [0] => 2011-01-22 11:23:19
            [1] => SHAR开发者_JAVA技巧E_TWEET
        )

    [3] => Array
        (
            [0] => 2011-01-22 11:23:19
            [1] => SHARE_FACEBOOK
        )

and many other different values (about 10), what I want to do is I want to count the number of times a string is in the array. I was going to use array_count_values but it doesn't count multidimensional arrays.

Any other options?


This could be done by first flattening the array, and then using array_count_values() on it:

For flattening, here is the trick:

$array = call_user_func_array('array_merge', $arrays);

And then:

$counts = array_count_values($array);

Output:

array (
  'Date' => 1,
  'Action' => 1,
  '2011-01-22 11:23:19' => 2,
  'SHARE_TWEET' => 1,
  'SHARE_FACEBOOK' => 1,
)

Full code:

$array = call_user_func_array('array_merge', $arrays);
var_export(array_count_values($array));


Any time you're dealing with arrays, especially with loops in PHP I can't string enough suggest you look at the array documentation, You'd be suprised how quickly you realise most of the loops in your code is unnecessary. PHP has a built in function to achieve what you're after called array_walk_recursive. And since you're using PHP5 you can use closures rather that create_function (which can be very troublesome, especially to debug, and can't be optimised by the PHP interpreter afik)

$strings = array();
array_walk_recursive($arr, function($value, $key) use (&$strings) { 
    $strings[$value] = isset($strings[$value]) ? $strings[$value]+1 : 1; 
});   

I know, unary statements aren't always clear, but this one is simple enough, but feel free to expand out the if statement.

The result of the above is:

print_r($strings);
Array
(
    [Date] => 1,
    [Action] => 1,
    [2011-01-22 11:23:19] => 2,
    [SHARE_TWEET] => 1,
    [SHARE_FACEBOOK] => 1,
)


Pseudo Code

$inputArray = // your array as in the example above

foreach ($inputArray as $key => $value) {
  $result[$value[1]] = $result[$value[1]] + 1;
}

var_dump($result);


Here is a way to do the job:

$arr = Array (
    1 => Array (
            0 => 'Date',
            1 => 'Action'
    ),
    2 => Array (
            0 => '2011-01-22 11:23:19',
            1 => 'SHARE_TWEET'
    ),
    3 => Array (
            0 => '2011-01-22 11:23:19',
            1 => 'SHARE_FACEBOOK'
    )
);

$result = array();
function count_array($arr) {
global $result;
    foreach($arr as $k => $v) {
        if (is_array($v)) {
            count_array($v);
        } else {
            if (isset($result[$v])) {
              $result[$v]++;
            } else {
              $result[$v] = 1;
            }
        }
    }
}
count_array($arr);
print_r($result);

output:

Array
(
    [Date] => 1
    [Action] => 1
    [2011-01-22 11:23:19] => 2
    [SHARE_TWEET] => 1
    [SHARE_FACEBOOK] => 1
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜