How to convert array to multiple variables in PHP

2019-09-06 02:07发布

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.

标签: php arrays hash
2条回答
SAY GOODBYE
2楼-- · 2019-09-06 02:43

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-09-06 03:01

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);
查看更多
登录 后发表回答