Trying to sort an array of arrays using php .. hair pulling?

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] ....]

+4
source share
1 answer

Try it. This seems to work fine here:

 <?php $array = array( array(0,1), array(2,1) , array(4,1) , array(4,1) , array(5,1) ,array(9 , 1) , array(9, 1) , array(10,1) , array(12,1), array(13,1) ); $last_elem = null; foreach($array as &$elem){ if($last_elem){ if($elem[0] == $last_elem[0]){ $elem[2] = (isset($last_elem[2]) ? $last_elem[2] + 1 : 1); $elem[1] = $last_elem[1] + 1; } } $last_elem = &$elem; } var_dump($array); ?> 

This works fine here, I tested it with this array:
[ [0,1], [2,1], [4,1], [4,1], [4,1], [4,1], [5,1], [9,1], [9,1] ,[9,1] ,[10,1] ,[12,1], [13,1] ];

Output:

 array 0 => array 0 => int 0 1 => int 1 1 => array 0 => int 2 1 => int 1 2 => array 0 => int 4 1 => int 1 3 => array 0 => int 4 1 => int 2 2 => int 1 4 => array 0 => int 4 1 => int 3 2 => int 2 5 => array 0 => int 4 1 => int 4 2 => int 3 6 => array 0 => int 5 1 => int 1 7 => array 0 => int 9 1 => int 1 8 => array 0 => int 9 1 => int 2 2 => int 1 9 => array 0 => int 9 1 => int 3 2 => int 2 10 => array 0 => int 10 1 => int 1 11 => array 0 => int 12 1 => int 1 12 => & array 0 => int 13 1 => int 1 
+2
source

All Articles