开发者

Grouping data in array

I want to group data in PHP like so:

I have an array with dumped stuff in it

Array (
    [0] => Array (
            [0] => 123
            [1] => 3000
            [2] => 595
            [3] => 1005
        )

    [1] => Array (
            [0] => 53
            [1] => 700
            [2] => 195
            [3] => 315
        )

    [2] => Array (
            [0] => 82
开发者_StackOverflow            [1] => 142
            [2] => 342
            [3] => 640
        )

    [3] => Array (
            [0] => 82
            [1] => 702
            [2] => 200
            [3] => 320
        )
)

This array represents items in 3D. What I want to achieve is to group them by coordinates so that if (roughly) they are close together (let's say closer than 10 units on each axis).

So far I came up with following script: http://pastebin.com/wJ5HpTPL however it seems be failing - it groups some records for no reason at all and don't group those that I want. And I know how inefficient it is however I tried several methods that was failing and it evolved into that crappy code.

There is about 2000 of items however the script is going to be run for administrative purposes and is hosted on my local server so performance is not important.


To calculate the difference you can use Pythagoras' theorem and:

Grouping data in array

It's explained really well here: http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/


I have resolved my problem by simply using $row = $bulk[$i]; in my code instead of $row = next($bulk); which for some reason was causing problems.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜