Search code examples
phparrayspositioningheatmap

Count an array php


I have the following data:

a  , b , c   , d                 , e  , f
375, 52, 1892, http://example.com, ::1, 1308233412
341, 52, 1892, http://example.com, ::1, 1308233412
422, 52, 1892, http://example.com, ::1, 1308233417
478, 50, 1892, http://example.com, ::1, 1308233418
58, 481, 1892, http://example.com, ::1, 1308233432
69, 481, 1892, http://example.com, ::1, 1308233432
487, 49, 1892, http://example.com, ::1, 1308233432
  • a = position y
  • b = position x
  • c = screen resolution (browser)
  • d = host
  • e = ip
  • f = timestamp

what do i want to do is, for example:

check if it in a 50x50px box if so count +1.

so i would have a table like:

y/x |  0 | 50  | 100  | 150
----+----+-----+------+----
50  | 0  |  1  |   2  |   0
100 | 0  |  0  |   1  |   0
150 | 1  |  0  |   0  |   1
200 | 2  |  2  |   3  |   0
etc.

Hoping somebody can help me to achieve this above

he following link is creating a heatmap, http://www.design-code.nl/example/heatmap.php , but the heatmap is overlapping so i want to put the green dots in an array which is counted, and those area's where it is within 50x50 will highlight with an other color. Sorry for the poor information


Solution

  • While this question is poorly worded, I think I understand what you are asking for. Here's what you should do. I am not all that fluent in php so please make sure that you look over the code snippets that I write instead of copy/pasting them.

    1. Find the maximum X and Y values.
    2. Instantiate and initialize a 2D array based on those values divided by 50.

    For instance if you wanted Array[X][Y] you would do:

    $myArray = array();
    for ($x = 0; $x < $maxX / 50; $x++) {
        $myArray[] = array();
        for ($y = 0; $y < $maxY / 50; $y++) {
            $myArray[$x][] = 0;
        }
    }
    

    This should instantiate and initialize a 2D array to all 0's just like you need

    3) Iterate through your array and for each entry, increment the value of $myArray[$curX/50][$curY/50] by 1:

    foreach ($inputArray as $curRow) $myArray[$curRow[b]/50][$curRow[a]/50] += 1;
    

    Again, I'm no pro at php and have really just started working with it, but this should be a good start.