0

I have an array:

$r = array(1,2,42,55);

and I want to call encrypt(); function of hashids

which takes input like this:

encrpyt(1,2,42,55);

I tried extract($r) but it does not work.

jeff
  • 13,055
  • 29
  • 78
  • 136

2 Answers2

0

You can call a callback on each of the elements of the array. check array_map if it helps.

black sensei
  • 6,528
  • 22
  • 109
  • 188
0

It's ugly, but there's this:

eval("encrypt(" . implode(",", $r) . ");");

Here's your obligatory reminder that eval is potentially dangerous and to be used rarely if ever!

Edit: Forgot about call_user_func_array. That's your answer! Sample code:

$r = array(1,2,42,55);
$hashids = new Hashids\Hashids('this is my salt');
$hash = call_user_func_array(array($hashids, "encrypt"), $r);
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Seems nice. Thanks ! I don't have time to try now, I will do as soon as possible. – jeff Feb 16 '14 at 22:31
  • Ok please don't forget to mark as accepted if it works for you. Interesting to note that [PHP 5.6 has solved this problem for you](http://docs.php.net/manual/en/migration56.new-features.php). `$hashids->encrypt(...$r);` – miken32 Feb 18 '14 at 23:47