php_guest Posted March 14, 2011 Share Posted March 14, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/ Share on other sites More sharing options...
AbraCadaver Posted March 14, 2011 Share Posted March 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/#findComment-1187437 Share on other sites More sharing options...
php_guest Posted March 14, 2011 Author Share Posted March 14, 2011 tnx, but if(empty(${"_$var"}[trim($field)])){ doesn't work. Seems "_$var is not ok because if I put instead of $var a string "GET", than works. Quote Link to comment https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/#findComment-1187501 Share on other sites More sharing options...
.josh Posted March 15, 2011 Share Posted March 15, 2011 Why not just pass the array to the function to begin with? Quote Link to comment https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/#findComment-1187551 Share on other sites More sharing options...
php_guest Posted March 15, 2011 Author Share Posted March 15, 2011 Why not just pass the array to the function to begin with? You mean filter_input_array()? Because if not, than won't solve my problem which is how to pass $method Quote Link to comment https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/#findComment-1187566 Share on other sites More sharing options...
.josh Posted March 15, 2011 Share Posted March 15, 2011 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")) Quote Link to comment https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/#findComment-1187569 Share on other sites More sharing options...
php_guest Posted March 15, 2011 Author Share Posted March 15, 2011 tnx, this works! Quote Link to comment https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/#findComment-1187701 Share on other sites More sharing options...
AbraCadaver Posted March 15, 2011 Share Posted March 15, 2011 Why not just pass the array to the function to begin with? Ha ha, why did I not think of that? Quote Link to comment https://forums.phpfreaks.com/topic/230615-convert-string-to-array-name/#findComment-1187804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.