trying to sort a array of arrays using php.. pulling hair out?
I am in the middle of doing a quick prototype of some statistical graphs using FLOT and php and I am literally hacking something together to show something.
However I am faced with a problem which is doing my nut...
so I have some data which I have grabbed from the database and then in order to make it work easily for my Flot hack I have 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 end plan is to have several sets of data and then produce a stacked chart. so what i need to do is sort out where there are duplicate like in the above where there is:
[.....[4,1],[4,1] .....]
needs to look like this:
[.....[4,1],[4,2,1].....]
below is my attempt at trying to sort it ( it is the latest iteration of attempt i have tried forwards, backwards changing inner values and outer values)...
$count = count($array);
$sorted = false;
while (!$sorted)
{
$doneSomething = 0;
for($i = $count - 1; $i > 0; $i--)
{
$tempArr = $array[$i开发者_StackOverflow中文版];
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 this:
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 resolve this issue it would be really appreciated.
thanks
Vade
Edit: i should note that if there is only 2 duplicates it isnt that bad i can work that out but its when there is 3 or more ie:
[....[4,1],[4,1],[4,1],[4,1]...]
which needs to look like:
[....[4,1],[4,2,1],[4,3,2],[4,4,3]....]
Try this out. This seems to be working 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 is working perfectly here, I have 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] ];
The output is:
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
精彩评论