Tsarma, are you sure the code is correct, I changed the input in the example below (changed the order) and now the output is different. You need to sort the array first if this is undesired...
This is what Tsarma posted as the correct code. (in case the external website ever goes down...) I changed the input by switching two lines...
Code:
<?php
function sortArray($array){
$tempArray = array();
foreach($array as $key => $value) {
if (!in_array($value,$tempArray)) {
$tempArray = checkThisString($value,$tempArray);
}
}
return $tempArray;
}
function checkThisString($myStr,$myArray) {
$temp1Array = array();
$checkStr = '';
$temp1Array = explode('/',$myStr);
for ($i = 0; $i < count($temp1Array); $i++) {
if (!empty($checkStr)) {
$checkStr = $checkStr.'/'.$temp1Array[$i];
}
else {
$checkStr = $temp1Array[$i];
}
if (in_array($checkStr,$myArray)) {
return $myArray;
}
}
$myArray[] = $myStr;
return $myArray;
}
$array =Array
(
'folder1/folder2/images2.jpg',
'folder1/folder2',
'folder1/folder2/images1.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',
'files.txt'
);
print_r(sortArray($array));
?>
As I understood it is supposed to generate:
Code:
Array
(
[0] => folder1/folder2
[1] => folder1/folder3
[2] => folderA
[3] => folderB/folderB/folderB
[4] => folderC/file1.pdf
[5] => folderC/file2.pdf
[6] => folderD/.htaccess
[7] => files.txt
)
But the output is:
Code:
Array
(
[0] => folder1/folder2/images2.jpg
[1] => folder1/folder2
[2] => folder1/folder3
[3] => folderA
[4] => folderB/folderB/folderB
[5] => folderC/file1.pdf
[6] => folderC/file2.pdf
[7] => folderD/.htaccess
[8] => files.txt
)
Bookmarks