开发者

Count an array php

开发者 https://www.devze.com 2023-03-13 21:47 出处:网络
I have the following data: a, b , c, d, e, f 375, 52, 1892, http://example.com, ::1, 1308233412 3开发者_StackOverflow41, 52, 1892, http://example.com, ::1, 1308233412

I have the following data:

a  , b , c   , d                 , e  , f
375, 52, 1892, http://example.com, ::1, 1308233412
3开发者_StackOverflow41, 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


Okay, I think I've worked out what the question is about (see my comment by the question above).

The way I'd do it is to divide the X and Y positions by 50, and use the floor() function to get an integer value from that. This would be the box number that they would be in.

You can then populate this into an array quite easily.

The following code will produce the array you need:

$map = array();
foreach($data as $row) {
    $map[floor($row['x']/50)][floor($row['y']/50)]++;
}

Then you can just print it into a table (with the row and column headings being the cell number multiplied by 50).

You might want to zero-fill the $map array beforehand so that you get zeros in cells where you don't have any hits, or you can work this out when you print it; up to you)


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.


The coordinates of the box would be:

$x_coor = floor ($a / 50);
$y_coor = floor ($b / 50);

With the coordinates you can them full a n-dimensional array


You want to look for a space-filling-curve. A sfc is usually used for heatmaps and reduce the 2d complexity to a 1d complexity. You want to look for Nick's hilbert curve spatial quadtree blog.


first, you need to fill an array full with 0's:

$result = array();
for($i=0;$i<2000;$i+=50){
    $result[$i] = array();
    for($j=0;$j<2000;$j+=50){
        $result[$i][$j] = 0;
    }
}

where 2000 is the maximum screen width/height. then, you count your values, where $a is your array:

foreach($a as $v){
    $result[floor($v[0]/50)*50][floor($v[1]/50)*50]++;
}
0

精彩评论

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