我正在使用FLOT和php做一些统计图的快速原型,我实际上是在一起黑客攻击以展示一些东西.
然而,我面临的问题是我的坚果……
所以我有一些数据,我从数据库中抓取,然后为了让它很容易为我的Flot hack工作,我把它转换成如下所示:
[[0,1],[2,1],[4,1],[4,1],[5,1],[9,1],[9,1],[10,1],[ 12,1],[13,1]]
现在最终计划是拥有多组数据,然后生成堆积图表.所以我需要做的是理清上面有重复的地方,如上所述:
[….. [4,1],[4,1] …..]
需要看起来像这样:
[….. [4,1],[4,2,1] …..]
下面是我尝试对它进行排序的尝试(这是尝试前进的尝试的最新迭代,向后改变内在价值和外部价值)……
$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;
}
结果是这样的:
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
)
)
你可以看到不是我想要的结果:
[[0,1],[2,1],[4,1],[4,2,1],[5,1],[9,1],[9,2,1],[10, 1],[12,1],[13,1]]
如果有人可以帮我解决这个问题,我们将非常感激.
谢谢
瓦杰
编辑:我应该注意,如果只有2个重复,它不是那么糟糕我可以工作,但它有3或更多时,即:
[… [4,1],[4,1],[4,1],[4,1] …]
需要看起来像:
[…. [4,1],[4,2,1],[4,3,2],[4,4,3] …]
最佳答案 试试吧.这似乎在这里工作正常:
<?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);
?>
这在这里工作得很好,我用这个数组测试了它:
[[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]];
输出是:
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