使用list()或类似方法实现左侧分配,同时保留除第一个参数外的所有其他参数的数组 [英] Using list() or similar to achieve left-hand assignment while conserving array for all other parameters than the first
问题描述
我知道标题没有透露。如果有解决方案,则可能涉及一些体面的欺骗。
The title doesn't give it away, i know. If there is a solution to this, it probably involves some decent trickery.
情况
我将通过代码进行解释:
I'll explain via code:
$str = 'all.your.base';
list($a, $b, $c) = explode('.', $str);
$ a
, $ b
和 $ c
将进行相应设置。
$a
, $b
and $c
will be set accordingly.
但是,我想要实现的更像是这样:
However, what i want to achieve is more like this:
$str = 'all.your.base';
list($a, $rest) = explode('.', $str);
之后, $ a
应该包含'all'
和 $ rest
应该是一个看起来像这样的数组: ['your', 'base']
Whereas afterwards $a
should contain 'all'
and $rest
should be an array that looks like this: ['your', 'base']
很显然,这可以通过其他方式轻松完成,但是随着我写了几行代码到达那里,我想知道是否没有像我可能没有意识到的解决方案,例如 list()
,它可以(通过一些技巧)在较短的时间内提供所需的结果,更快更整洁的方式?
Clearly, this can be done easily by other means, but as i wrote a few lines of code to get there, i was wondering if there is not a solution at hand such as list()
that maybe i am unaware of, which can – with some trickery – provide the desired result in a shorter, quicker and neater fashion?
推荐答案
使用第三个参数来爆炸
, limit
Use the third argument to explode
, limit
$str = 'all.your.base';
list($a, $rest) = explode('.', $str, 2);
这篇关于使用list()或类似方法实现左侧分配,同时保留除第一个参数外的所有其他参数的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!