从php中的S3对象数组中删除项目

时间:2014-04-04 13:24:25

标签: php arrays amazon-s3

我有一个这样的数组:

Array
(
    [0] => Accounts.csv
    [1] => Book1.xlsx
    [2] => Documents/
    [3] => Documents/AdbWinApi.dll
    [4] => Documents/AdbWinUsbApi.dll
    [5] => Documents/adb.exe
    [6] => Documents/boot.img
    [7] => Documents/fastboot.exe
    [8] => Documents/side_banner.jpg
    [9] => Documents/source.properties
    [10] => File Organization.docx
    [11] => How to Manage Risks.docx
    [12] => How to Trade Forex.docx
    [13] => Ken Doc - Galad Letter Head.pdf
    [14] => Ken Header.png
    [15] => MX_2004_fwmx_2004_en.exe
    [16] => xx.docx
    [17] => asmack-master.zip
    [18] => ca5rmhx7l4-Human Heart 2.7z
    [19] => evasi0n7.exe
    [20] => ken header.pdf
    [21] => sp42471.exe
)

我希望它返回如下所示的数组;

Array
(
    [0] => Accounts.csv
    [1] => Book1.xlsx
    [2] => Documents
    [3] => File Organization.docx
    [4] => How to Manage Risks.docx
    [5] => How to Trade Forex.docx
    [6] => Ken Doc - Galad Letter Head.pdf
    [7] => Ken Header.png
    [8] => MX_2004_fwmx_2004_en.exe
    [9] => xx.docx
    [10] => asmack-master.zip
    [11] => ca5rmhx7l4-Human Heart 2.7z
    [12] => evasi0n7.exe
    [13] => ken header.pdf
    [14] => sp42471.exe
)

我正在使用以下代码;

{

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

foreach($ls as $l) {

$fname = str_replace($prefix,"",$l['name']);

   if(!empty($fname)) { 
     $rv[] = $fname; 
   }
  } 
}
   if(!empty($rv)) { 
     return $rv; 
  }
}

}

我可以对代码进行哪些更改以获得上述结果。我使用DonovanSchönknechtAmazonS3库进行codeigniter。

2 个答案:

答案 0 :(得分:1)

您可以使用foreach

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

}
print_r($newarr);

Working Demo

答案 1 :(得分:0)

您可以使用$array[3] = "";覆盖元素,例如然后你有空数组元素然后你可以使用这样的函数:

    function empty_array($array){
    foreach($array as $ar => $value) {
    if($value == '') {
    unset($array[$ar]);
    }
    }
    return $array;
    } 

    $new_array = emtpy_array($old_array);