A
A
Alexander2015-08-05 03:40:59
PHP
Alexander, 2015-08-05 03:40:59

How to simply cluster the values ​​of a one-dimensional array?

Let's have an array like this:

array(
  1000 => 12,
  1010 => 20,
  1085 => 34,
  1100 => 40,
  1200 => 35,
  1350 => 20,
  1510 => 20,
  2000 => 22,
  2100 => 24,
  2350 => 40,
  3500 => 14,
  4500 => 20,
  6000 => 25
)

It is necessary to break it into sane groups of key intervals. How?
For understanding:
These are the prices of goods (keys) and the quantity of goods at a given price (values). Filter spacing needed. With an arbitrary approximate step. The data set is not real of course, but for an example.
The problem is that let's say if we have a step of 500r. then the first interval will be 1000-1500 and close goods will fall out of it at 1510 rubles. But they will fall into the interval 1500-2000, but the goods for 2010 will be pumped out into the interval 2000-2500.
If a person beat, he would make intervals 1000-1510, 2000-2010 or 2000-2550, etc.
How to do it automatically?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
heartdevil, 2015-08-05
@heartdevil

Hello.
I don't really understand what you are not satisfied with what you described?
And what do you end up wanting?
If you have a step of 500 rubles, and you start with 1000, then you will have exactly intervals with a step of 500. Everything that does not fall into the intervals will fall into the following intervals.
It turns out that you set the step, but you are waiting for a completely different interval.

A
AlikDex, 2015-08-05
@AlikDex

add % of it to the 2nd value of the interval. For example 3-5%. Then the sum 2050 from the interval 1500-2000 will fall into it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question