Combine duplicate entries in array / foreach

Tags: , ,



I am trying to combine two entries either on the array level or foreach level (either would work fine)

Here is the code:

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

} 

Here is the output:

folder
 -
general
 -BMS-Shield.txt
general
 -image-2.jpg
planning
 -composer.json
test
 -

Here is the desired output:

folder

general
 -BMS-Shield.txt
 -image-2.jpg
planning
 -composer.json
test

Just for a bit of background I am trying to get a list of files from aws s3 and display the contents of each folder (‘folder’, ‘general’, ‘planning’ etc…) but when there is more than one file in a folder it prints the folder twice (as seen in output above).

Here is the file structure on s3:

image of file structure

I have tried the array_unique() function but this gave the same output.

Any help would be much appreciated.

Output of $objects[‘Contents’]:

Array
(
    [0] => Array
        (
            [Key] => dashboard/demo.consultive.test/user-data/nXb2K1feviqsjzuW7mgQG2Cghc1Kem8U/uploads/folder/
            [LastModified] => AwsApiDateTimeResult 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] => AwsApiDateTimeResult 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] => AwsApiDateTimeResult 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] => AwsApiDateTimeResult 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] => AwsApiDateTimeResult Object
                (
                    [date] => 2020-02-25 09:08:36.000000
                    [timezone_type] => 2
                    [timezone] => Z
                )

            [ETag] => "d41d8cd98f00b204e9800998ecf8427e"
            [Size] => 0
            [StorageClass] => STANDARD
        )

)

Output of $folders:

Array
(
    [0] => test
)

Answer

You can do it by giving your $folders array the key as the folder name, and as the value an array of subfolders :

$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>';

Disclaimer : The code is working and is giving you an idea for a better logic. It’s not optimized nor is it the best solution. But it is A solution.

https://wtools.io/php-sandbox/b0dL



Source: stackoverflow