Results 1 to 9 of 9

Thread: PHP Array Filter help

  1. #1

    Default PHP Array Filter help

    I need an algorithm to reduce 1st kind of array to 2nd Array, Any input is welcome.

    1st Array
    Code:
    Array
    (
    	[0] => tl_files/cd_album
    	[1] => tl_files/cd_album/images1.jpg
    	[2] => tl_files/cd_album/images2.jpg
    	[3] => tl_files/cd_album/cover
    	[4] => contao
    	[5] => contao/install.php
    	[6] => contao/file.php
    	[7] => contao/index.php
    	[8] => ajax.php
    	[9] => templates
    	[10] => templates/.htaccess
    	[11] => templates/music_academy.sql
    )
    2nd Array
    Code:
    Array
    (
    	[0] => tl_files/cd_album
    	[1] => contao
    	[2] => ajax.php
    	[3] =>templates
    )
    Basically if a folder is selected then ignore/remove all the containing elements (files and folders).

    Thanks
    OM MANI PEME HUNG! how many has to die for freedom and dignity. Save this world

  2. #2
    User
    Join Date
    07-26-09.
    Posts
    175

    Default 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?

  3. #3
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default 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.

  4. #4
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default 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;
    }

  5. #5

    Default 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
    OM MANI PEME HUNG! how many has to die for freedom and dignity. Save this world

  6. #6
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default 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.

  7. #7

    Default 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
    OM MANI PEME HUNG! how many has to die for freedom and dignity. Save this world

  8. #8
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default 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
    )

  9. #9

    Default 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.
    OM MANI PEME HUNG! how many has to die for freedom and dignity. Save this world

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •