在 php 中解压一组参数 [英] unpacking an array of arguments in php

查看:25
本文介绍了在 php 中解压一组参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 提供了*"运算符,用于解包元组列表并将它们作为参数提供给函数,如下所示:

Python provides the "*" operator for unpacking a list of tuples and giving them to a function as arguments, like so:

args = [3, 6]
range(*args)            # call with arguments unpacked from a list

这相当于:

range(3, 6)

有谁知道是否有办法在 PHP 中实现这一点?在谷歌上搜索PHP Unpack"的变体并没有立即发现任何东西......也许它在 PHP 中被称为不同的东西?

Does anyone know if there is a way to achieve this in PHP? Some googling for variations of "PHP Unpack" hasn't immediately turned up anything.. perhaps it's called something different in PHP?

推荐答案

您可以使用 call_user_func_array() 来实现:

You can use call_user_func_array() to achieve that:

call_user_func_array("range", $args); 使用您的示例.

这篇关于在 php 中解压一组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆