1

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

样本结果:

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);

    }
4

3 回答 3

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
)
于 2012-11-21T07:34:00.670 回答
2

这不是很好地使用http://us2.php.net/manual/en/function.array-unique.php

于 2012-11-21T07:45:54.227 回答
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];
        }
    }
于 2012-11-21T07:34:27.353 回答