PHP列表函数的可选参数
我的代码中有这样一行:
I have a line like this in my code:
list($user_id, $name, $limit, $remaining, $reset) = explode('|', $user);
最后 3 个参数可能存在也可能不存在.如果数组小于预期,是否有类似于 list 的函数会自动忽略那些最后的参数?
The last 3 parameters may or may not be there. Is there a function similar to list that will automatically ignore those last parameters if the array is smaller than expected?
推荐答案
只需在字符串末尾添加一些备用管道即可:
Just add some spare pipes to the end of the string:
list($user_id, $name, $limit, $remaining, $reset) = explode('|', $user.'||||');
问题解决了.
注意:如果您正在加载任意以竖线分隔的数据,您可能需要使用 str_getcsv()
函数而不是 explode()
.
Note: If you're loading arbitrary pipe-delimited data, you might want to use str_getcsv()
function rather than explode()
.
相关文章