array issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kcpaige89
Forum Newbie
Posts: 6
Joined: Sat May 12, 2007 4:13 pm

array issues

Post by kcpaige89 »

I have an array that will have several possiblities, such as a1-a12 and b1-b48 and c1-c128 yadda yadda.

My question would be, if each of these represents a different unique index.html file in a seperate directory,

ie, ./this/place/b1/index.html + /this/place/b2/index.html
but /that/place/a1/index.html

so, all the b numbers are found in /this/place/ and all the a numbers are found in /that/place/

is there a way to make a switch statement include that file?

my psedo-code

Code: Select all

$some_value = the desired page, such as a11
switch (array($some_value)){
        case array(any a number value):

        include ("/that/place/any a number value/index.html");
        break;

        case array(any b number value):
        include ("/this/place/any b number value/index.html");
        break;
}
the goal of this is to avoid several hundred switch cases, like a case for a1 a2 a3 a4 and so on.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Is the actual array index a1, a2 and so on?

Or is it more like

Code: Select all

Array (
    0 => 'a1',
    2 => 'a2',
    3 => 'a4',
    etc
)
If that's the case, I would populate my array differently so as to avoid a switch altogether, e.g.

Code: Select all

Array (
    'a1' => 0,
    'a2' => 0,
    ....
    'c5' => 0,
    etc
)
This way you can do a simple isset() and then check for the directory / file, e.g.

Code: Select all

$some_value = 'a11';
if ( isset($array[$some_value]) && is_dir("/that/place/{$some_value}")  ) {
    include_once("/that/place/{$some_value}/index.html");
}
That's it, 1 line.
Post Reply