user1037355
user1037355

Reputation:

how to turn associative array into function parameters in php

Is there a way to turn an associative array into parameters for a function.

I have a simply array such:

$arr = [
  'id' => 321654,
  'name' => 'action1'
];

I have a static function in a class:

class someclass{
  static function someFunction( $id, $name ){
     //the rest of the method
  }
}

I can call the class by variables, eg:

$class = 'someclass';
$method = 'somFunction';

return $class::$method(  );

I can also pass is in a definite qty of function parameters, or an array

return $class::$method( $arr );

In this example's case I could hard code the params to:

return $class::$method( $arr['id'], $arr['name'] );

But how would i pass an unknown qty of keys. Another run may contain 1 key or 4 or 10...

Upvotes: 0

Views: 87

Answers (1)

user1037355
user1037355

Reputation:

Thanks to @Rizier123 comment:

This worked very nicely:

call_user_func_array( ''.$class.'::'.$method, $arr );

Upvotes: 1

Related Questions