php 计算数组的个数

发布于 2024-11-16 03:57:15 字数 1083 浏览 3 评论 0原文

我有以下数据:

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 = 位置 y
  • b = 位置 x
  • c = 屏幕分辨率(浏览器)
  • d = 主机
  • e = ip
  • f = 时间戳

我想要做的是,例如:

检查它是否在 50x50px 框中(如果是)数+1。

所以我会有一个像这样的表格:

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.

希望有人可以帮助我实现上面的目标,

他下面的链接正在创建一个热图, http://www.design-code.nl/example/heatmap.php,但是热图是重叠的,所以我想将绿点放在一个被计数的数组中,这些区域就是它在 50x50 范围内用另一种颜色突出显示。抱歉信息贫乏

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吾性傲以野 2024-11-23 03:57:15

好吧,我想我已经弄清楚了问题所在(请参阅我对上面问题的评论)。

我的方法是将 X 和 Y 位置除以 50,然后使用 floor() 函数从中获取整数值。这将是它们所在的盒子编号。

然后您可以轻松地将其填充到数组中。

以下代码将生成您需要的数组:

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

然后您可以将其打印到表格中(行标题和列标题是单元格编号乘以 50)。

您可能需要预先对 $map 数组进行零填充,以便在没有任何命中的单元格中得到零,或者您可以在打印时解决这个问题;由你决定)

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)

狼性发作 2024-11-23 03:57:15

虽然这个问题措辞不好,但我想我明白你的要求。这是你应该做的。我不太精通 php,所以请确保您查看我编写的代码片段,而不是复制/粘贴它们。

  1. 找到最大 X 和 Y 值。
  2. 根据这些值除以 50 实例化并初始化一个 2D 数组。

例如,如果您想要 Array[X][Y] 您会这样做:

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

这应该实例化并初始化一个 2D 数组到所有0 就像您需要的那样

3) 迭代数组,对于每个条目,将 $myArray[$curX/50][$curY/50] 的值增加 1:

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

同样,我不是 php 专业人士,而且刚刚开始使用它,但这应该是一个好的开始。

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.

偏闹i 2024-11-23 03:57:15

盒子的坐标将是:

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

通过坐标,您可以将它们填满一个 n 维数组

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

孤独岁月 2024-11-23 03:57:15

您想要寻找一条空间填充曲线。 sfc 通常用于热图,并将 2d 复杂度降低到 1d 复杂度。您想要查找 Nick 的希尔伯特曲线空间四叉树博客。

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.

花开浅夏 2024-11-23 03:57:15

首先,您需要用 0 填充数组:

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

其中 2000 是最大屏幕宽度/高度。然后,计算您的值,其中 $a 是您的数组:

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

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]++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文