Jump to content

convert string to array name


php_guest

Recommended Posts

Hi everybody,

 

I need some help about how to convert string to array name. I have the following function which pass $_REQUEST type as parameter. It is not working because I don't know how to convert $method to $_GET or $_POST in a proper way. Do anybody know how?

 

function validateIfFilled($method, $fields)
{  
    $exploded=explode(",",$fields);    
    foreach($exploded as $field){                             
        if(empty($method[trim($field)])){          
          return false;      
        }
    }
    return true;
};

if(!validateIfFilled('$_GET', "email, password, address"))
...

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/
Share on other sites

You might like filter_input() or filter_input_array(), but for your problem, there are several ways.  Here's one:

 

if(empty(${"_$var"}[trim($field)])){

 

And then...

 

if(!validateIfFilled('GET', "email, password, address")){

 

But I would probably pass in fields as an array and skip the explode.

no, I mean passing $_GET to your function instead of "GET" and then trying to get "GET" to turn into $_GET.

 

function validateIfFilled($method, $fields)
{  
    $exploded=explode(",",$fields);    
    foreach($exploded as $field){                             
        if(empty($method[trim($field)])){          
          return false;      
        }
    }
    return true;
};

// pass the $_GET array as an array (not some "GET" string)
if(!validateIfFilled($_GET, "email, password, address"))

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.