1

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?

$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.

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.

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"
0

2 Answers 2

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;
}

Ideone demo

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I'm sure you could. It just requires dividing the problem into smaller problems, until the small problems are trivial to solve. By the way I've simplified the code quite a bit (I was reinventing array_slice - I usually don't use PHP very much).
0

For a technique with fewer total function calls, you might use a regex pattern to collect potentially-overlapping segments of a predefined length in a loop. Demo or Demo with no pre-loop push

$string = '1 2 3 4 5';
$result[] = $string;
for ($i = substr_count($string, ' ') - 1; $i >= 0; --$i) {
    preg_match_all("#(?=(\S+(?: \S+){{$i}}))#", $string, $m); // get overlapping segments
    array_push($result, ...$m[1]); // accumulate as flat array
}
var_export($result);

Output:

array (
  0 => '1 2 3 4 5',
  1 => '1 2 3 4',
  2 => '2 3 4 5',
  3 => '1 2 3',
  4 => '2 3 4',
  5 => '3 4 5',
  6 => '1 2',
  7 => '2 3',
  8 => '3 4',
  9 => '4 5',
  10 => '1',
  11 => '2',
  12 => '3',
  13 => '4',
  14 => '5',
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.