Answer the question
In order to leave comments, you need to log in
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);
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
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.
$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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question