Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] array and array keys


[SOLVED] array and array keys

Reply
  #1 (permalink)  
Old 05-05-08, 01:39 AM
xportal xportal is offline
Newbie Coder
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation [SOLVED] array and array keys

i have this array
PHP Code:

$fileArray = array(


        
'WAP' => array(
            
'Errors.class.php',
            
'Index.class.php',
            
'Signup.class.php',
            
'Login.class.php',
            
'Home.class.php'
        
),
        
'MOB' => array(
            
'Errors.class.php',
            
'Index.class.php',
            
'Signup.class.php',
            
'Login.class.php',
            
'Home.class.php'
        
),
        
'WEB' => array(
            
'Errors.class.php',
            
'Index.class.php',
            
'Signup.class.php',
            
'Login.class.php',
            
'Home.class.php'
        
)
); 
this function
PHP Code:

$func->checkBrowser(); 

which will return WAP MOB or WEB depending on the browser.

i want to include the files in the arrays in a loop if the array key == the value returned from the function to check browser. how do ii go about doing this?

your help will be really appreciated.
Reply With Quote
  #2 (permalink)  
Old 05-05-08, 02:06 AM
xportal xportal is offline
Newbie Coder
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
never mind

never mind i figured it out

PHP Code:

foreach($fileArray as $key => $file)

{
    if(
$key==$func->checkBrowser())
    {
        for(
$i=0;$i<4;$i++)
        {
            if(
file_exists($sources_dir.$file[$i]))
            {
                require_once(
$sources_dir.$file[$i]);
            }
        }
    }

Reply With Quote
  #3 (permalink)  
Old 05-05-08, 02:33 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
This could be simplified a bit:
PHP Code:

$browser $func->checkBrowser();

$files   $fileArray[$browser];

foreach (
array_filter($files'is_file') AS $file)
{
    include 
$file;

Reply With Quote
  #4 (permalink)  
Old 05-05-08, 02:50 AM
xportal xportal is offline
Newbie Coder
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Wink

Quote:
Originally Posted by Nico View Post
This could be simplified a bit:
PHP Code:

$browser $func->checkBrowser();

$files   $fileArray[$browser];

foreach (
array_filter($files'is_file') AS $file)
{
    include 
$file;

that didnt seem to work but this gets it done
PHP Code:

foreach($fileArray as $browser => $file)

{
    if(
$browser==$func->checkBrowser())
    {
        for(
$i=0;$i<count($file);$i++)
        {
            if(
file_exists($sources_dir.$file[$i]))
            {
                require_once(
$sources_dir.$file[$i]);
            }
        }
    }

thanks anyway
Reply With Quote
  #5 (permalink)  
Old 05-05-08, 02:55 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Maybe 'cause I forgot to add the $sources_dir to the include().
Reply With Quote
  #6 (permalink)  
Old 05-05-08, 03:11 AM
xportal xportal is offline
Newbie Coder
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
i'm looking for an easier way to do this
PHP Code:

// Gets $ac or set to 'index' if not defined

$ac = isset($_REQUEST['ac']) ? $_REQUEST['ac'] : 'index';

// $REQUEST['ac']array -  $REQUEST['ac']=>array('class','method');

if($func->checkBrowser()=='MOB'// set mobile array else set other array
{
    
$acArray = array(
        
'index'  => array(
                        
'Index','display'
                    
),
        
'signup' => array(
                        
'Signup','display'
                    
),
        
'login'  => array(
                        
'Login','display'
                    
),
        
'home'   => array(
                        
'Home','display'
                    
)
    );
    
}else if(
$func->checkBrowser()=='WAP'//set wap array
{
    
$acArray = array(
        
'index'  => array(
                        
'Index','display'
                    
),
        
'signup' => array(
                        
'Signup','display'
                    
),
        
'login'  => array(
                        
'Login','display'
                    
),
        
'home'   => array(
                        
'Home','display'
                    
)
    );
    
}else{
    
//other/web array
    
$acArray = array(
        
'index'  => array(
                        
'Index','display'
                    
),
        
'signup' => array(
                        
'Signup','display'
                    
),
        
'login'  => array(
                        
'Login','display'
                    
),
        
'home'   => array(
                        
'Home','display'
                    
)
    );
}

// Checks if the array key (action request)($REQUEST['ac']) is in $acArray 
if(!array_key_exists($ac,$acArray))
{
    
$Errors->PGerror();
    exit();
}

// Creates object
$Object = new $acArray[$ac][0];

// Instance of object
$Object->$acArray[$ac][1](); 
can you assist me please
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Implode Array keys Jay6390 PHP 6 12-23-07 05:21 PM
Make A New Array From An Old Array (Excluding 1 Array Element) w2n PHP 14 08-17-07 03:24 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 03:04 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 07:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.