I am in the middle of a quick prototype of some statistical graphs using FLOT and php, and I literally hack something together to show something.
However, I ran into a problem that makes my nut ...
so I have some data that I captured from the database, and then, to make it easy to work for my Flot hack, I converted it to look something like this:
[[0,1], [2,1], [4,1], [4,1], [5,1], [9, 1], [9, 1], [10,1], [ 12.1], [13.1]]
now the final plan is to have multiple datasets and then create a complex diagram. so what I need to do is sort where there is a duplicate, as in the above where there is:
[..... [4,1], [4,1] .....]
should look like this:
[..... [4,1], [4,2,1] .....]
below is my attempt to try to sort it (this is the last iteration of the attempt I tried forward, backward changing internal values ββand external values) ...
$count = count($array); $sorted = false; while (!$sorted) { $doneSomething = 0; for($i = $count - 1; $i > 0; $i--) { $tempArr = $array[$i]; foreach($array as $key => $a) { if($key == $i) { echo "breaking"; continue; } $result = array_diff($tempArr,$a); if(count($result) == 0 ) { $array[$i][1]++; if(count($array[$i]) == 3) $array[$i][2]++; else $array[$i][] = 1; $doneSomething++; } if($doneSomething > 0) break; } } if($doneSomething == 0) $sorted=true; }
The result is the following:
Array ( [0] => Array ( [0] => 0 [1] => 1 ) [1] => Array ( [0] => 2 [1] => 4 [2] => 3 ) [2] => Array ( [0] => 4 [1] => 2 [2] => 1 ) [3] => Array ( [0] => 4 [1] => 5 [2] => 4 ) [4] => Array ( [0] => 5 [1] => 1 ) [5] => Array ( [0] => 9 [1] => 2 [2] => 1 ) [6] => Array ( [0] => 9 [1] => 3 [2] => 2 ) [7] => Array ( [0] => 10 [1] => 1 ) [8] => Array ( [0] => 12 [1] => 1 ) [9] => Array ( [0] => 13 [1] => 1 ) )
As you can see, not really my intended result:
[[0,1], [2,1], [4,1], [4,2,1], [5,1], [9,1], [9,2,1], [10, 1], [12.1], [13.1]]
If anyone can help me solve this problem, it will be really appreciated.
thanks
Pocket
Edit: I have to note that if there are only two duplicates, it is not so bad, I can handle it, but when there are 3 or more, i.e.:
[.... [4,1], [4,1], [4,1], [4,1] ...]
which should look like this:
[.... [4,1], [4,2,1], [4,3,2], [4,4,3] ....]