删除阵列中的冗余数据

时间:2012-11-21 07:19:35

标签: php arrays

我遇到了问题,我在下面有这个结果,我想删除相同的时间戳。

示例结果:

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

期待输出

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

我正在考虑使用explode然后使用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);

    }

3 个答案:

答案 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
)

答案 1 :(得分:2)

答案 2 :(得分: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];
        }
    }