<?php
function reduceFilenameArray($array_1 = array()) {
$array_2 = array();
foreach( $array_1 as $val) {
// If appears as a directory name, add a filename to the end
if (strpos(basename($val), '.') !== false){
$newVal = dirname($val);
} else {
$newVal = dirname($val.'/.');
}
// filenames without directories should be kept according to the example
$newVal = $newVal == '.' ? $val : $newVal;
// Build the new array by adding unique values only
if (!in_array($newVal, $array_2)) {
$array_2[] = $newVal;
}
}
return $array_2;
}
$array1 =Array
(
'folder1/folder2',
'folder1/folder2/images1.jpg',
'folder1/folder2/images2.jpg',
'folder1/folder3',
'folderA',
'folderA/folderA1/folderA2/A.jpg',
'folderA/folderA3/someFile.jpg',
'folderB/folderB/folderB',
'folderB/folderB/folderB/somefile.txt',
'folderB/folderB/folderB/somefolder',
'folderC/file1.pdf',
'folderC/file2.pdf',
'folderD/.htaccess',
);
print_r(reduceFilenameArray($array1));
/* Suggested Solution should be.
Array
(
[0] => folder1/folder2
[1] => folder1/folder3
[2] => folderA
[3] => folderB/folderB/folderB
[4] => folderC/file1.pdf
[5] => folderC/file2.pdf
[6] => folderD/.htaccess
)
*/
?>