0

How do I count how much times a number is in the 2 dimensional table ?

Code example:

array (size=3)
  0 => 
    array (size=7)
      0 => int 19
      1 => int 17
      2 => int 37
      3 => int 3
      4 => int 17
      5 => int 11
      6 => int 23
  1 => 
    array (size=7)
      0 => int 16
      1 => int 30
      2 => int 16
      3 => int 4
      4 => int 24
      5 => int 14
      6 => int 15
  2 => 
    array (size=7)
      0 => int 11
      1 => int 41
      2 => int 30
      3 => int 11
      4 => int 10
      5 => int 18
      6 => int 36

i'm trying to find out how to search how many times each number is in the table. This is what I got :

$arrayWeeknummer = array();

echo "<table>";
for ($i = 0; $i < 3; $i++)
{
    echo "<tr>";
    for($j = 0; $j < 7; $j++)
    {
        $arrayWeeknummer[$i][$j] = rand(1,42);
        echo "<td>".$arrayWeeknummer[$i][$j]."</td>";
    }
    echo "</tr>";
}
echo "</table>";

for($k = 0; $k < 42; $k++)
{
    $aantal = 0;
    $newArray[$k][0] = $k+1;

    if(array_search($k+1, $arrayWeeknummer))
    {
        $newArray[$k][1] = $aantal++;
    }

$newArray[$k][1] = $aantal;
}

Unfortunately i'm doing something wrong in my if (array_search) statement since it never get called.

4
  • 1
    can't you just use array_count_values? Commented Apr 30, 2015 at 9:04
  • so you want to find the occcurence of number in each week ? Commented Apr 30, 2015 at 9:07
  • the occurence of a number over all weeks Commented Apr 30, 2015 at 9:12
  • array_walk_recursive() should allow you to simplify walking through the array Commented Apr 30, 2015 at 9:21

3 Answers 3

1

This should work for you:

Here I just go through all elements with 2 foreach loop and then check if the value is already in my $result array, if not I initialize it with 0 and then increment it every time it is in the array.

<?php

    $result = [];

    foreach($arr as $a){
        foreach($a as $v){
            if(!isset($result[$v]))
                $result[$v] = 0;
            $result[$v]++;
        }
    }

    print_r($result);

?>

Or if you want to use array_walk_recursive() just do it like this:

$result = [];

array_walk_recursive($arr, function($v, $k)use(&$result){
    if(!isset($result[$v]))
        $result[$v] = 0;
    $result[$v]++;
});
Sign up to request clarification or add additional context in comments.

4 Comments

@Awa You're welcome! (BTW: Added also the solution if you want to use array_walk_recursive(), maybe even a bit less code)
The big benefit of using array_walk_recursive() is that it can handle as many dimensions as the array holds, not simply 2
What If I want an array of 42 elements and not only the elements which are in table ?
@Awa Then initialize $result with this: $result = array_combine(range(1,42), array_fill(0, 42, 0));
0

What I would do is to make an array with keys from 1 to 42 and with values initialized at 0. Then simply run a for loop into another for loop to look up every value in your array, and simply increment the value corresponding to the value in your array going from 1 to 42.

Edit : seems like array_count_values() does the same

Comments

0
<?php 

    $input = [
    [
            0 =>  19,
            1 =>  17,
            2 =>  37,
            3 =>  3,
            4 =>  17,
            5 =>  11,
            6 =>  23],
            [
            0 =>  45,
            1 =>  1,
            2 =>  17,
            3 =>  34,
            4 =>  18,
            5 =>  19,
            6 =>  26],
    ];

    $max = 0;

    foreach($input as $array) {
        $max = max(max($array), $max);
    }

    $result = array_fill (0, $max, 0);

    foreach($input as $array) {
        foreach($array as $number) {
            $result[$number]++;
        }
    } 

print_r($result);

2 Comments

OP's array is multidimensional
Edited as multidimentional

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.