php - Adding null values to an array -


i have method:

public function search($searchkey=null, $summary=null, $title=null, $authors=null, $paginationpage=0) {      ...  } 

and i'm trying retrieve parameters this:

$class = new search();  // parameters $reflectionmethod = new \reflectionmethod($class, "search"); try {     foreach($reflectionmethod->getparameters() $parameter) {         if(array_key_exists($parameter->name, $this->params)) {             $parameters[$parameter->name] = $this->params[$parameter->name];     } elseif($parameter->isdefaultvalueavailable()) {         $paramaters[$parameter->name] = $parameter->getdefaultvalue();     } else {             ...     } } catch(\exception $e) {         ... }     // call function return call_user_func_array(array($class, "search"), $parameters); 

my $this->params has content:

array   'paginationpage' => int 2   'id' => int 30   'searchkey' => string 'test' (length=4) 

because $summary, $title , $authors not present, default value null. when assigning null value argument, skipped results in $parameters array looks this:

array   'searchkey' => string 'test' (length=4)   'paginationpage' => int 2 

which result in method call like:

public function search('test', 2, null, null, 0) {          ...  } 

while should be:

public function search('test', null, null, null, 2) {          ...  } 

hopefully see problem. how can make sure null values put $parameters array. adding invalid value not possible, because user input, can everything.

edit

in example above method search hardcoded. 1 of simplified things search variable , because of search can anything. means don't know parameters of method , can't predefine them before foreach loop. solution of predefining parameters code should doing.

how pre-initializing $parameters before entering foreach loop:

$parameters = array(     $searchkey => null,     $summary => null,     $title => null,     $authors => null,     $paginationpage => 0 ); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -