将字符串转换为 php 参数
所以假设我有一个函数:
function j($a, $b){
return $a + $b;
}
然后我将函数的预期参数放入字符串中:
$str = '1,3';
有没有一种方法可以使函数将单个字符串参数视为程序员插入到函数中的参数......因此,当您执行
j($str)
时,
函数不会将 $str 视为单个字符串参数,而是获取字符串的内容并将其视为程序员编写的 j(1,3)
而不是j($str)
这也是一个相当简单的示例,但我希望它适用于更复杂的参数字符串,包括数组、多维数组、关联数组、数组中的数组,以及其中包含逗号的字符串参数(因此仅用逗号进行爆炸并不是真正可行)
我也不想在解决方案中使用 eval()
编辑
另外我试图让它在任何函数上工作,而不仅仅是我的函数(并且不仅仅是这个具体的函数(只是一个毫无价值的示例函数),因此最好在函数外部完成该操作
so suppose I have a function:
function j($a, $b){
return $a + $b;
}
and then I put the intended arguments of the function into a string:
$str = '1,3';
is there a way to make the function treat the single string argument as if it were the arguments that the programmer inserted into the function....so when you do
j($str)
,
instead of having the function treat the $str as a single string argument, it fetches the content of the string and treats it as if the programmer wrote j(1,3)
instead of j($str)
also this is a rather simple example, but I'd like it to work for even more complicated argument strings involving arrays, multidimensional arrays, associative arrays, array within arrays, as well as string arguments that have commas in it (so just exploding by comma is not really feasible)
also I'd rather not use eval() in the solution
EDIT
Also I'm trying to get this to work on any function not just mine (and not just this specific function which is just a worthless example function) so preferably the operation is to be done outside of the function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
http://www.php.net/call_user_func_array
我不知道你想如何让它工作“更复杂的参数字符串,包括数组的数组”,因为很难将它们表达为字符串。如果您可以将任何字符串格式化为数组,例如使用 JSON 字符串和 json_decode ,那么 call_user_func_array 就是您的朋友。
http://www.php.net/call_user_func_array
I have no idea how you want to make this work for "more complex argument strings including arrays of arrays", since it's hard to express those as strings. If you can format whatever string you have into an array though, for example using JSON strings and
json_decode
,call_user_func_array
is your friend.这应该适用于单个逗号分隔的字符串:
您想要在多维数组中求和什么?所有的值?
如果是:您只需检查参数的类型(例如使用 is_array()),然后迭代它。当它是多维时,递归调用你的函数。
This should work for a single, comma-separated string:
What do you want to sum in a multi-dimensional array? All of the values?
If yes: You only have to check the type of your argument (e.g. using is_array()) and then iterate through it. When it is multi-dimensional, call your function recursively.
将除第一个参数之外的所有参数设为可选,然后使用
list()
和explode()
如果第一个参数是字符串(或包含,
):这只是一个基本的例子,但原理应该很清楚:
make all parameter but except the first one optional and then use
list()
andexplode()
if the first parameter is a string (or contains,
):this is just a basic example, but the principle should be clear:
现在您的函数将支持两种格式,带有一个参数,例如:
以及两个参数:
Now your function will support both formats, with one argument eg:
as well as two arguments:
这有效:)
检查一下:
This works :)
Check it:
大多数答案都假设所有内容都用逗号“,”很好地分隔开,但情况并非总是如此。例如,可能有不同的变量类型,甚至是带有逗号的字符串。
这可以通过 php 7 中的 eval() 和点的“...”参数检索来处理。
Most of the answers assume, that everything is nicely separated with coma ",", but it's not always the case. For example there could be different variable types, even strings with coma's.
This can be handled with eval() and dot's "..." args retrieval in php 7.