arrays and strings in a for() {}

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
Flux
-


Joined: 13 Oct 2006
Posts: 48

PostPosted: Sun Apr 22, 2007 6:57 pm    Post subject: arrays and strings in a for() {} Reply with quote

So let's say I have to make a few directories in succession.
Directory that exists: /files/user/pics/
Directory that ultimately needs to exist: /files/user/pics/oneday/thatevent/thistime/
I have mkdir().
I have the string $subfolder1 = "oneday/thatevent/thistime".
I have the array $subfolder = explode( "/" , $subfolder1).

I want to cause this to happen:
Code:
mkdir("/files/user/pics/oneday");
mkdir("/files/user/pics/oneday/thatevent");
mkdir("/files/user/pics/oneday/thatevent/thistime");


I need to find a php script version of, basically, this:
so let's say we have:
Code:
for ($i = 0; $i < count($subfolder); $i++) {
mkdir("/files/user/pics".$sub);
}

How do I get $sub to equal in the first instance $subfolder[0], then the second $subfolder[0]."/".$subfolder[1] etc. etc. until I reach the final piece of the array and the script stops? Note that I ask with the intent to not have a limit on how many there are in the array/the number of pieces of array are not set in stone/I don't know how many pieces to the array there will be.
_________________
My signature is so lame.

I think phpBB forums hates my laptop '.';
Back to top View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Mon Apr 23, 2007 4:57 pm    Post subject: Re: arrays and strings in a for() {} Reply with quote

Code:

$sub = "/files/user/pics";
for ($i = 0; $i < count($subfolder); $i++) {
$sub = $sub . "/" . $subfolder[$i];
mkdir($sub);
}

_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
Flux
-


Joined: 13 Oct 2006
Posts: 48

PostPosted: Wed Apr 25, 2007 12:05 am    Post subject: Reply with quote

smart is me..or not.
Thanks again aprelium.
_________________
My signature is so lame.

I think phpBB forums hates my laptop '.';
Back to top View user's profile Send private message
cmxflash
-


Joined: 11 Dec 2004
Posts: 872

PostPosted: Wed Apr 25, 2007 12:36 pm    Post subject: Reply with quote

You should have a look at foreach() instead.

Code:
<?php

$subs = array("./test", "./test/hello", "./test/hello/world");

foreach($subs as $key => $value) {
   mkdir($value);
   echo "Created: ".$key." - ".$value."<br />";
}

?>
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group