我正在尝试在数组级别或foreach级别上组合两个条目(都可以正常工作)
代码如下:
foreach ($objects['Contents'] as $object) {
$_object_ = str_replace($prefix, '', $object['Key']);
$_date_ = str_replace('+00:00', '', str_replace('T', ' ', $object['LastModified']));
$folders = array();
if (strpos($_object_, '/') !== false && substr($_object_, -1) != '/') {
if (!in_array(strtok($_object_, '/'), $folders)) {
array_push($folders, strtok($_object_, '/'));
}
} elseif (strpos($_object_, '/') !== false) {
array_push($folders, str_replace('/', '', $_object_));
}
echo '<pre>';
foreach($folders as $folder){
echo $folder.'<br/>';
if (strpos($_object_ , $folder.'/') !== false) {
echo ' -'.str_replace($folder.'/', '', $_object_);
}
}
echo '</pre>';
}
以下是输出:
folder
-
general
-BMS-Shield.txt
general
-image-2.jpg
planning
-composer.json
test
-
这是所需的输出:
folder
general
-BMS-Shield.txt
-image-2.jpg
planning
-composer.json
test
仅出于一点背景,我试图从aws s3获取文件列表并显示每个文件夹的内容(“文件夹”,“常规”,“计划”等),但是当还有更多内容时而不是文件夹中的一个文件,它将打印该文件夹两次(如上面的输出所示)。
这是s3上的文件结构:
我尝试了array_unique()
函数,但是给出了相同的输出。
任何帮助将不胜感激。
$ objects ['Contents']的输出:
Array
(
[0] => Array
(
[Key] => dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/folder/
[LastModified] => Aws\Api\DateTimeResult Object
(
[date] => 2020-02-25 09:47:43.000000
[timezone_type] => 2
[timezone] => Z
)
[ETag] => "d41d8cd98f00b204e9800998ecf8427e"
[Size] => 0
[StorageClass] => STANDARD
)
[1] => Array
(
[Key] => dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/general/BMS-Shield.txt
[LastModified] => Aws\Api\DateTimeResult Object
(
[date] => 2020-02-25 10:24:58.000000
[timezone_type] => 2
[timezone] => Z
)
[ETag] => "5270e0e4f58c9b33eb7fc6f4cb3c8a04"
[Size] => 2747
[StorageClass] => STANDARD
)
[2] => Array
(
[Key] => dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/general/image-2.jpg
[LastModified] => Aws\Api\DateTimeResult Object
(
[date] => 2020-02-24 16:34:21.000000
[timezone_type] => 2
[timezone] => Z
)
[ETag] => "1c6eaa239899913d732a4bca343a1eac"
[Size] => 57667
[StorageClass] => STANDARD
)
[3] => Array
(
[Key] => dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/planning/composer.json
[LastModified] => Aws\Api\DateTimeResult Object
(
[date] => 2020-02-24 16:34:45.000000
[timezone_type] => 2
[timezone] => Z
)
[ETag] => "b83a5f582075d5668c8bb092b2e20516"
[Size] => 62
[StorageClass] => STANDARD
)
[4] => Array
(
[Key] => dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/test/
[LastModified] => Aws\Api\DateTimeResult Object
(
[date] => 2020-02-25 09:08:36.000000
[timezone_type] => 2
[timezone] => Z
)
[ETag] => "d41d8cd98f00b204e9800998ecf8427e"
[Size] => 0
[StorageClass] => STANDARD
)
)
$ folders的输出:
Array
(
[0] => test
)
答案 0 :(得分:1)
您可以通过将$folders
数组的键作为文件夹名称,并将值作为子文件夹的数组作为键来实现:
$objects = [
[
'Key' => 'dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/folder/',
'LastModified' => [
'date' => '2020-02-25 09:47:43.000000',
'timezone_type' => '2',
'timezone' => 'Z'
],
'ETag' => 'd41d8cd98f00b204e9800998ecf8427e',
'Size' => 0,
'StorageClass' => 'STANDARD'
],
[
'Key' => 'dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/general/BMS-Shield.txt',
'LastModified' => [
'date' => '2020-02-25 10:24:58.000000',
'timezone_type' => '2',
'timezone' => 'Z'
],
'ETag' => '5270e0e4f58c9b33eb7fc6f4cb3c8a04',
'Size' => 0,
'StorageClass' => 'STANDARD'
],
[
'Key' => 'dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/general/image-2.jpg',
'LastModified' => [
'date' => '2020-02-24 16:34:21.000000',
'timezone_type' => '2',
'timezone' => 'Z'
],
'ETag' => '1c6eaa239899913d732a4bca343a1eac',
'Size' => 0,
'StorageClass' => 'STANDARD'
],
[
'Key' => 'dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/planning/composer.json',
'LastModified' => [
'date' => '2020-02-24 16:34:45.000000',
'timezone_type' => '2',
'timezone' => 'Z'
],
'ETag' => 'b83a5f582075d5668c8bb092b2e20516',
'Size' => 62,
'StorageClass' => 'STANDARD'
] ,
[
'Key' => 'dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/test/',
'LastModified' => [
'date' => '2020-02-25 09:08:36.000000',
'timezone_type' => '2',
'timezone' => 'Z'
],
'ETag' => 'd41d8cd98f00b204e9800998ecf8427e',
'Size' => 0,
'StorageClass' => 'STANDARD'
]
];
$prefix = 'dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/';
$folders = [];
foreach ($objects as $object) {
$sub_folders = array();
$_object_ = str_replace($prefix, '', $object['Key']);
$_date_ = str_replace('+00:00', '', str_replace('T', ' ', $object['LastModified']));
if (strpos($_object_, '/') !== false && substr($_object_, -1) != '/') {
if (!in_array(strtok($_object_, '/'), $folders)) {
$folders[strtok($_object_, '/')][] = explode('/', $_object_)[1];
}
} elseif (strpos($_object_, '/') !== false) {
$folders[str_replace('/', '', $_object_)] = '';
}
}
echo '<pre>';
foreach($folders as $key => $folder){
echo $key.'<br/>';
if (is_array($folder)) {
foreach($folder as $f) {
echo ' -'.$f;
echo '<br/>';
}
} else {
echo '<br/>';
}
}
echo '</pre>';
免责声明:代码正在运行,并且为您提供了更好的逻辑思路。它没有优化,也不是最佳解决方案。但这是一个解决方案。