Re: PHP Array Filter help
Where do you get this array from? Maybe you can use glob() function to retrieve only 1st level of a folder?
Re: PHP Array Filter help
As Tru says - It would certainly be easier to NOT produce the first array in the first place.
Otherwise maybe comparing using "in_array", or maybe you would have to split each array on "/" and compare chunks, or make two arrays (one containing anything with a slash) and use array_diff_associate (or similar) to remove one from the other if the first chunk is duplicated?
Let us know if you can't do what Tru suggested.
Re: PHP Array Filter help
I have an inefficient example for you. But it might not work straight away because I did not test it. There is one question I have because your example seems inconsistent: tl_files/cd_album should not be reduced to tl_files, yet tl_files/cd_album/cover is reduced to tl_files/cd_album. I didn't know what to do with that.
Code:
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;
}
Re: PHP Array Filter help
@tru | @ramjet: Array is just an example, i have just made it myself. Sorry for the confusion. I thought x/y, x/y/z.jpg would be more confusing.
@Ruud : Thanks for the effort. But it didn't work in 2 cases
1. folder/.htaccess
2. if its - folder/file1.jpg,folder/file2.jpg your script gave out - folder
Another example is here http://codepad.org/AfgZllGP
Re: PHP Array Filter help
Ooh, now I see what you are after. You need to find out which folders are selected because any files that are in this folder will be selected by default. On top of that you need all files that are not included in any of the selected folders.
Just change the code I made a bit. First sort the array alphabetically on value. Then for each parent folder check if that isn't inside the new array yet.
Re: PHP Array Filter help
Once again thanks a lot.
Here is a link to a solution, as someone else helped me on this.
http://codepad.org/9SeQxJdx
Re: PHP Array Filter help
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
)
Re: PHP Array Filter help
Thanks for pasting the code and pointing out it is needed to sort the array first. I've notice it afterwards too.
What do you think about the solution? I'm still not really very satisfied as it might tend to become inefficient when the path is deep nested. e.g a/b/c/d/e/x.file it has to explode on / and loop through them all.
There might be some better solution out there.