V
V
valves2014-10-05 09:36:14
PHP
valves, 2014-10-05 09:36:14

How to improve the for code?

There is another error in the code where the if function begins, I can not understand what the problem is.

$figures = preg_split("-[\W]-u", "1 2 3 4");
$bas = $i = [];

for ($a = 0; $a <= count($figures); $a++) {
    for ($b = 0; $b < $a; $b++) {
        $c = array(implode(' ', array_slice($figures, $b, count($figures) - $a + 1)));

        if (!isset($i[$c])) {
            $i[$c] = array_push($bas, $c);
        }
    }
}

print_r($bas);

you need to complete the code to such a state
Array
(
    [0] => Array
        (
            [0] => 1 2 3 4
        )

    [1] => Array
        (
            [0] => 1 2 3
      [1] => 4
        )

    [2] => Array
        (
      [0] => 1
            [1] => 2 3 4
        )

    [3] => Array
        (
            [0] => 1 2
      [1] => 3 4
        )

    [4] => Array
        (
      [0] => 1
            [1] => 2 3
      [2] => 4
        )

    [5] => Array
        (
      [0] => 1 2
            [1] => 3 4
        )

    [6] => Array
        (
            [0] => 1
        )

    [7] => Array
        (
            [0] => 2
        )

    [8] => Array
        (
            [0] => 3
        )

    [9] => Array
        (
            [0] => 4
        )

)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
N
Nazar Mokrinsky, 2014-10-05
@nazarpc

First, replace single-letter variables with normal ones.

S
Sergey, 2014-10-05
Protko @Fesor

How to collect a multilevel array?

B
bromzh, 2014-10-05
@bromzh

Take the debugger in your hands and do everything line by line, you will most likely find an error much faster than strangers will delve into your code and fix it.
You can get by with a low-budget version of the debugger - output all values ​​to standard output or somewhere else.

A
Alexander, 2014-10-06
@itvdonsk

$i[$c]
Are you pushing an array into the key?

Arrays (type array) and objects (type object) cannot be used as keys. Using it this way will generate a warning: Illegal offset type.

X
Xakephp, 2014-10-07
@Xakephp

The issue has long been resolved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question