从php中的S3对象多维数组中删除重复项

时间:2014-04-07 07:03:10

标签: php arrays amazon-s3

我遇到了下面数组的问题,因为它没有显示我想要的方式;

  Array
(
    [0] => Array
        (
            [name] => Accounts.csv
            [time] => 1396518749
            [size] => 4685
            [hash] => 4ddde7286adcbf06472a7d8f9987bf88
        )

[1] => Array
    (
        [name] => Book1.xlsx
        [time] => 1396436339
        [size] => 9776
        [hash] => 96f219be0397b240777969f8b3cbb711
    )

[2] => Array
    (
        [name] => Documents/
        [time] => 1396510415
        [size] => 0
        [hash] => d41d8cd98f00b204e9800998ecf8427e
    )

[3] => Array
    (
        [name] => Documents/AdbWinApi.dll
        [time] => 1396847400
        [size] => 96256
        [hash] => 47a6ee3f186b2c2f5057028906bac0c6
    )


[4] => Array
    (
        [name] => ken header.pdf
        [time] => 1396436334
        [size] => 110772
        [hash] => fe64e53a2237c7b84b08981ed2c0447a
    )

[5] => Array
    (
        [name] => sp42471.exe
        [time] => 1396527931
        [size] => 128609088
        [hash] => 1687428c91011b0c028f91d16593c5e7
    )

我想删除一些重复的项目并重新构建输出,如下所示;

  Array
(
    [0] => Array
        (
            [name] => Accounts.csv
            [time] => 1396518749
            [size] => 4685
            [hash] => 4ddde7286adcbf06472a7d8f9987bf88
        )

[1] => Array
    (
        [name] => Book1.xlsx
        [time] => 1396436339
        [size] => 9776
        [hash] => 96f219be0397b240777969f8b3cbb711
    )

[2] => Array
    (
        [name] => Documents
        [time] => 1396510415
        [size] => 0
        [hash] => d41d8cd98f00b204e9800998ecf8427e
    )



[3] => Array
    (
        [name] => ken header.pdf
        [time] => 1396436334
        [size] => 110772
        [hash] => fe64e53a2237c7b84b08981ed2c0447a
    )

[4] => Array
    (
        [name] => sp42471.exe
        [time] => 1396527931
        [size] => 128609088
        [hash] => 1687428c91011b0c028f91d16593c5e7
    )

我正在使用下面的代码来检索数组。

private function listFiles($bucket = null , $prefix = null) {
    $ls = S3::getBucket($bucket, $prefix);
        if(!empty($ls))  {

            foreach($ls as $l) {

                //print_r($ls);

                $fname = str_replace($prefix,"",$l['name']);
                $ftime = $l['time'];
                $fsize = $l['size'];
                $fhash = $l['hash'];


                if(!empty($fname)) { 
                    $rv[] = array('name' => $fname, 'time' => $ftime, 'size' => $fsize, 'hash' => $fhash); 
                    //$rv[] = $fname; 
                }
            } 
        }
    if(!empty($rv)) { 
        return $rv; 
    }
}

我应该修改哪一部分才能获得上述结果。 注意:我仍然是阵列的新手。

2 个答案:

答案 0 :(得分:1)

你可以这样继续。

$newarr=array();
foreach($yourarr as $k=>&$arr)
{
    $v=$arr['name'];
    if(strpos($v,'/')!==false)
    {
        $v=explode('/',$v);
        if(isset($v[1]) && strlen($v[1])>0){}else{$arr['name']=$v[0];$newarr[]=$arr;}
    }
    else { $newarr[]=$arr;}

}
print_r($newarr);

<强> OUTPUT :

Array
(
    [0] => Array
        (
            [name] => Accounts.csv
            [time] => 1396518749
            [size] => 4685
            [hash] => 4ddde7286adcbf06472a7d8f9987bf88
        )

    [1] => Array
        (
            [name] => Book1.xlsx
            [time] => 1396436339
            [size] => 9776
            [hash] => 96f219be0397b240777969f8b3cbb711
        )

    [2] => Array
        (
            [name] => Documents
            [time] => 1396510415
            [size] => 0
            [hash] => d41d8cd98f00b204e9800998ecf8427e
        )

    [3] => Array
        (
            [name] => ken header.pdf
            [time] => 1396436334
            [size] => 110772
            [hash] => fe64e53a2237c7b84b08981ed2c0447a
        )

    [4] => Array
        (
            [name] => sp42471.exe
            [time] => 1396527931
            [size] => 128609088
            [hash] => 1687428c91011b0c028f91d16593c5e7
        )

)

Working Demo

答案 1 :(得分:0)

由于您没有指定要过滤的确切内容,我假设重复项是子文件夹中的任何内容。

所以这是一种过滤掉这些的可能方法:

private function listFiles($bucket = null , $prefix = null) {
    $ls = S3::getBucket($bucket, $prefix);
        if(!empty($ls))  {
            $rv = array();
            foreach($ls as $l) {

                //print_r($ls);

                $fname = str_replace($prefix,"",$l['name']);
                $ftime = $l['time'];
                $fsize = $l['size'];
                $fhash = $l['hash'];

                $fname = trim($fname, "/");
                if(!empty($fname)) { 
                    $dup = false;
                    foreach($rv as $val)
                    {
                        $array = explode("/", $val['name']);
                        if ($array[0] == $fname)
                        {
                            $dup = true;
                            break;
                        }
                    }
                    if (!$dup)
                    {
                        $rv[] = array('name' => $fname, 'time' => $ftime, 'size' => $fsize, 'hash' => $fhash); 
                        //$rv[] = $fname; 
                    }
                }
            } 
        }
    if(!empty($rv)) { 
        return $rv; 
    }
}