Answer the question
In order to leave comments, you need to log in
PHP: how to group elements of an array that are close in value?
There is an array like:
Array
(
[0] => Array
(
[posY] => 5
[posX] => 48.75
[id] => 36
)
[1] => Array
(
[posY] => 5
[posX] => 52.5
[id] => 35
)
[2] => Array
(
[posY] => 5.5
[posX] => 18.75
[id] => 46
)
[3] => Array
(
[posY] => 7.5
[posX] => 52.5
[id] => 29
)
[4] => Array
(
[posY] => 15
[posX] => 45
[id] => 49
)
[5] => Array
(
[posY] => 16
[posX] => 75
[id] => 15
)
)
Array
(
[1] => Array
(
[0] => Array
(
[posY] => 5
[posX] => 48.75
[id] => 36
)
)
[2] => Array
(
[0] => Array
(
[posY] => 5
[posX] => 52.5
[id] => 35
)
[1] => Array
(
[posY] => 5.5
[posX] => 18.75
[id] => 46
)
[2] => Array
(
[posY] => 7.5
[posX] => 52.5
[id] => 29
)
)
[3] => Array
(
[0] => Array
(
[posY] => 15
[posX] => 45
[id] => 49
)
[1] => Array
(
[posY] => 16
[posX] => 75
[id] => 15
)
)
)
Answer the question
In order to leave comments, you need to log in
An incomprehensible task.
First, there can be many solutions. For example, let borders be abs(dx) <= 5, abs(dy) <= 5. Points A=(0,0), B=(4,4), C=(7,7). Then the solution will be both {A,B},{C} and {A},{B,C}. Is one of them correct, or both?
What if the points A=(0.0), B=(4.4), C=(7.7), D=(11.11)? Is the solution {A},{B,C},{D} good? It has more groups than {A,B},{C,D}, but the central group is denser.
And finally, is the condition "posX && posY in a group different from each other by no more than +\- some value" the only one? If yes, it is enough to create N groups, one element in each. The condition will be met. And if not - try to explain why this particular solution is not suitable. State the condition that is violated. Maybe then it will become clearer what kind of task it is. In the meantime, this is just a wish "make me beautiful."
And if the task is not formalized - look for the word "clustering". A lot has been written about her.
deltaX=abs(posX0-posX1)
deltaY=abs(posY0-posY1)
further - bring the functions to your code according to the structure.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question