I have a string of 5 numbers, and I need to separate them and store them in an array. I've been trying to come up with a function to do this for hours, unsuccessfully, so I'm wondering if anyone have any ideas of accomplishing this, or if there is a build-in method to do this?
我有一个5个数字的字符串,我需要将它们分开并将它们存储在一个数组中。我一直试图想出一个函数来做这个几个小时,但没有成功,所以我想知道是否有人有任何想法来完成这个,或者是否有一个内置方法来做到这一点?
$string = "1 2 3 4 5"
Required Outcomes
$array[0] = "1 2 3 4 5" (5)
$array[1] = "1 2 3 4" (4)
$array[2] = "2 3 4 5" (4)
$array[3] = "1 2 3" (3)
$array[4] = "2 3 4" (3)
$array[5] = "3 4 5" (3)
$array[6] = "1 2" (2)
$array[7] = "2 3" (2)
$array[8] = "3 4" (2)
$array[9] = "4 5" (2)
$array[10] = "1" (1)
$array[11] = "2" (1)
$array[12] = "3" (1)
$array[13] = "4" (1)
$array[14] = "5" (1)
P.S: The example string's numbers are 5 of them, but that's just an example. The working function should work with any amount of numbers. Thank you.
P.S:示例字符串的数字是其中的5个,但这只是一个例子。工作功能应该适用于任何数量的数字。谢谢。
Edit: I do not need a function to explode this. I already know the explode function. I'm asking for a function to get me the results below Required Outcomes no matter the amount of numbers. So if numbers are "1 2 3 4 5 6 7 8 9 10", the outcome would be like the patterns below require outcomes.
编辑:我不需要一个功能来爆炸这个。我已经知道了爆炸功能。无论数量多少,我都要求函数将结果发送到需求结果以下。因此,如果数字是“1 2 3 4 5 6 7 8 9 10”,结果将类似于以下模式需要结果。
The focus should be on the patterns below required outcomes. Those patterns are the patterns that I want to get. I need a function get get those exact patterns.
重点应放在低于要求结果的模式上。那些模式是我想要获得的模式。我需要一个函数来获取那些确切的模式。
More Examples
$string = "1 2 3 4"
$array[0] = "1 2 3 4"
$array[1] = "1 2 3"
$array[2] = "2 3 4"
$array[3] = "1 2"
$array[4] = "2 3"
$array[5] = "3 4"
$array[6] = "1"
$array[7] = "2"
$array[8] = "3"
$array[9] = "4"
1 个解决方案
#1
2
Here's a function which does this:
这是一个执行此操作的函数:
function do_the_thing($string) {
$numbers = explode(' ', $string);
$result = array();
for ($n = count($numbers); $n >= 1; --$n) {
for ($i = 0; $i <= count($numbers) - $n; ++$i) {
$result[] = implode(' ', array_slice($numbers, $i, $n));
}
}
return $result;
}
Basically:
-
$n
is the number count needed in a given pass. Goes from the number count down to 1. -
$i
is the start index at which a new result item should start copying from the input string. Starts at 0 and increments for each result item until the items's last number is the same as the input's last number.
$ n是给定传递中所需的数量。从数字计数下降到1。
$ i是新结果项应从输入字符串开始复制的起始索引。从0开始并为每个结果项递增,直到项的最后一个数与输入的最后一个数相同。
#1
2
Here's a function which does this:
这是一个执行此操作的函数:
function do_the_thing($string) {
$numbers = explode(' ', $string);
$result = array();
for ($n = count($numbers); $n >= 1; --$n) {
for ($i = 0; $i <= count($numbers) - $n; ++$i) {
$result[] = implode(' ', array_slice($numbers, $i, $n));
}
}
return $result;
}
Basically:
-
$n
is the number count needed in a given pass. Goes from the number count down to 1. -
$i
is the start index at which a new result item should start copying from the input string. Starts at 0 and increments for each result item until the items's last number is the same as the input's last number.
$ n是给定传递中所需的数量。从数字计数下降到1。
$ i是新结果项应从输入字符串开始复制的起始索引。从0开始并为每个结果项递增,直到项的最后一个数与输入的最后一个数相同。