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
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.

Link to comment
Share on other sites

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"))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.