Artron
Artron

Reputation: 266

Filter for many option

I have Input Variable :

$var_in="PDF Is from Adobe Acrobat";

i will strsub() 3 first and only get "PDF"

$key_is=substr($var_in,0,3);

I have some Main option, if found from $key_is

1.Option one with items :

"PDF", "XML", "XLS", "DOC"

will run somefunction1()

2.Option Two

"PNG", "BMP", "JPG"

run somefunction2()

3.Option Three

"DAT", "BAC", "SQL"

run somefunction3()

old ways is using

switch ($key_is) {
    case "PDF":
        runfunction1();
    break;
    case "DAT":
        runfunction3();
    break;
...
}

how can make it simple (Maybe some function)? if i add new items from main option is automatic goes to
suitable Function option selection.

like this, if i add "GIF" from main Option

"PNG", "BMP", "JPG", "GIF"

will run runfunction1() without i need change the switch

Upvotes: 1

Views: 37

Answers (4)

moni_dragu
moni_dragu

Reputation: 1163

you could build your arrays smart

$key_is = "PDF"

$array_opt1 = array("PDF", "XML", "XLS", "DOC");
$array_opt2 = array("PNG", "BMP", "JPG");
$array_opt3 = array("DAT", "BAC", "SQL");
$array_options = array('1'=> $array_opt1, '2'=> $array_opt2,'3'=> $array_opt3);

$array_func = array('1'=> 'funct1', '2'=> 'func2','3'=> 'func3');

foreach ($array_options as $key => $array_opt){
   if (in_array($key_is, $array_opt)){
       call_user_func($array_func[$key], $param); 
   }
}

$array_func[$key] will give you the name of the function to use. call_user_func will call the function. You can also pas a parameter if you need. If no parameter is needed leave the $param out.

EDIT: If the name of the functions are unique for each option you could even use the function name as key:

$array_options = array('funct1'=> $array_opt1, 'funct2'=> $array_opt2,'funct3'=> $array_opt3);

foreach ($array_options as $func_name => $array_opt){
   if (in_array($key_is, $array_opt)){
       call_user_func($func_name, $param); 
   }
}

Upvotes: 0

Janaka
Janaka

Reputation: 408

Using switch and in_array()

//set $test_type and start a switch

    switch($test_type) {
        case in_array($test_type, $array_opt1):{
            somefunction1();
            break;
        }
        case in_array($test_type, $array_opt2):{
            somefunction2();
            break;
        }
        case in_array($test_type, $array_opt3):{
            somefunction3();
            break;
        }
        default: {
            someOtherfunction();
            break;
        }
    }

Upvotes: 1

Janaka
Janaka

Reputation: 408

You can set $test_type ="your_type_name", and get actions according to type as below

 $test_type= "PDF";

        $array_opt1 = array("PDF", "XML", "XLS", "DOC");
        $array_opt2 = array("PNG", "BMP", "JPG");
        $array_opt3 = array("DAT", "BAC", "SQL");

        if(in_array($test_type, $array_opt1)){
         //send to relevant funcion for this group  
         somefunction1();
        }
        else if (in_array($test_type, $array_opt2)) {
           somefunction2();
        }else if(in_array($test_type, $array_opt3)){
           somefunction3();
        }else{
            someOtherfunction();
        }

Upvotes: 1

svantetic
svantetic

Reputation: 300

You can try inserting each option category to a category array. So you can have

$imageCategories = ['images', 'PNG','BMP','GIF'];
$dataCategories = ['data','PDF','XML','XLS'];

then pass an array to the function called for example

handleCategory($categoryArray)

and then check just first index of array to decide which function you should run

switch($categoryArray[0]) {
case 'images': 
   runFunction1();
   break;

Upvotes: 0

Related Questions