Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: PHP array loop

  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP array loop

    PHP Code:
    $listp explode("/"$path);//This splits the path
    for($i=0$i<(count($listp)); $i++){//This puts it into seperate arrays
        
    $paths[$i] = $listp[$i];
    }

    //There is an extra step here which just adds the '\' where necessary but eventually they are put into array 'a'

    $p[0] = $a[0];
    $p[1] = $a[0]."\\".$a[1];
    $p[2] = $a[0]."\\".$a[2].$a[3]; 
    I am creating a website that lists the music in a users selected directory. What i'm trying to do is loop through those directories and add them together (e.g. 1=1, 2=1+2, 3=1+2+3, etc.).
    Each of the sections in the directory selected is exploded at the "/" character, so $paths[0]="c:\", $paths[1]2="Users\" etc...
    How can i loop through $a and add them together?
    Last edited by Webos8668; 03-04-2013 at 11:27 PM.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    why do you need to explode the array?
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm using KendoUI and i'm exploding it so i can get the names of each folder to list in a tree. If i don't explode it then i can't list each individual folder. I just can't figure out now how to put them back together so each time you click a folder it would say where that is located (e.g. If they clicked Users then it would say "c:\Users")

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    how are they put into $a?
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This goes in the middle of the first code
    PHP Code:
    for($i=0$i<(count($listp)-1); $i++){
        if(!isset(
    $a)){
            
    $a[$i] = $listp[$i]."\\";
        }else{
            if(
    preg_grep('/\:$/i'$listp)){//add '\' where necessary
                
    $a[$i] = $listp[$i]."\\";
            }else{
                
    $a[$i] .= $listp[$i].$a[$i];
            }
        }


  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    does this not do that?

    PHP Code:
     $a[$i] .= $listp[$i].$a[$i]; 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #7
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    does this not do that?

    PHP Code:
     $a[$i] .= $listp[$i].$a[$i]; 
    No that returns this:
    PHP Code:
    array (size=4)
      
    => string 'C:\\' (length=4)
      
    => string 'Users' (length=5)
      
    => string 'david.sattler' (length=13)
      
    => string 'Music' (length=5
    when using var_dump($a);

    I looked at my code after your last post and it clicked i can use this code and it works:
    PHP Code:
    $a[$i] .= $a[($i-1)].$listp[$i]; 
    This returns:
    PHP Code:
    array (size=4)
      
    => string 'C:\\' (length=4)
      
    => string 'C:\\Users\\' (length=11)
      
    => string 'C:\\Users\\david.sattler\\' (length=26)
      
    => string 'C:\\Users\\david.sattler\\Music\\' (length=33
    when using var_dump($a);
    (the reason for so many backslashes is because of my javascript code...)


  •  

    Tags for this Thread

    Posting Permissions

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