2012-11-21 109 views
1

我有一个问题,我在下面有这个结果,我想删除相同的时间戳。删除阵列中的冗余数据

示例结果:

Array 
(
[0] => [1341100800000, 0] 
[1] => [1341100800000,85] 
[2] => [1343779200000,54] 
[3] => [1343779200000, 0] 
) 

期待输出

Array 
(
[0] => [1341100800000,85] 
[1] => [1343779200000,54] 
) 

我想使用然后爆炸SUBSTR函数来获取价值。这里是我想出了迄今为止..

$explode = array(); 
    foreach($string_format as $key => $value) { 
     $explode[] = explode(',', substr($value, 1)); 
     if((isset($explode[0]) && $explode[0][0] == $explode[1][0])) { 
      unset($explode[0]); 
     } 
     //print_r($explode); 

    } 
+0

发生了什么事的第二个值(0,85,0和54)?如果还有其他值,例如:0,85和95,该怎么办? – ariefbayu

+0

您的问题不完整。你如何选择要保留的重复值? –

回答

2
+1

是的,如果没有限制要保留哪个重复值(OP没有提及),可以使用一个简单的'array_unique($ the_array,SORT_NUMERIC)',利用PHP将字符串转换为数字和丢弃','之后的任何东西。 –

+0

这正是我用过的。 –

2

脚本:

foreach($string_format as $key => $value) { 
    $explode = explode(',', $value); 
    $unique[$explode[0]] = $value; 
} 

输入:

$string_format = array('0' => '1341100800000, 0', 
'1' => '1341100800000,85', 
'2' => '1343779200000,54', 
'3' => '1343779200000, 0'); 

输出:

$unique = 
Array 
(
    [1341100800000] => 1341100800000,85 
    [1343779200000] => 1343779200000, 0 
) 
0

这应该工作正常:)

$explode = array(); 
    $timestmp = array(); 

    foreach($string_format as $key => $value) { 
     $explode[$key] = explode(',', substr($value, 1)); 

     if(in_array($explode[$key][0], $timestmp)) { 
      unset ($explode[$key]); 
     } else { 
      $timestmp[]= $explode[$key][0]; 
     } 
    }