3

I have an array that looks like

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
        )

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?

2
  • There might be another function for it, but I'd recursively walk through the array with array_walk (pretty obvious, I guess) and check if a value is a string. Commented Jan 28, 2011 at 15:31
  • 1
    Please use echo var_export($array) instead of print_r($array), it's easier to use ;-) Commented Jan 28, 2011 at 15:38

4 Answers 4

5

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));
Sign up to request clarification or add additional context in comments.

Comments

1

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,
)

Comments

0

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);

Comments

0

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.