PHP - Problem with Array ()

1

I have the following code

$patron = '/{usuario}/photos/{photo_id}/comments/{comment_id}';

$array_patron = str_split($patron);
$array3 = array();
$variable_open = false;
$ind = -1;
foreach($array_patron as $i => $val){
    if($val === '{'){
        $variable_open = true;
        $ind++;
        continue;
    }else if($val === '}'){
        $variable_open = false;
        continue;
    }else if($variable_open){
        $array3[$ind] .= $val;  // Line number 22
    }
}
print_r($array3);

I do not understand why it results in NOTICE s

  

NOTICE Undefined offset: 0 on line number 22

     

NOTICE Undefined offset: 1 on line number 22

     

NOTICE Undefined offset: 2 on line number 22

Even though it shows me these NOTICE s, the resulting Array () is CORRECT .

Array (
    [0] => usuario
    [1] => photo_id
    [2] => comment_id
) 

Can anyone please explain to me what happens?

    
asked by Máxima Alekz 09.08.2017 в 22:21
source

2 answers

2

I found the solution:)

Discarding the solutions of @Ivan & @Marcos because they do not do what I'm waiting for.

  

Ivan's solution because it's manual, and it does not allow me to find more than   3 items between {}.

     

And Marcos' solution because I need the concatenation   obligatorily Otherwise it would only result in Array () whose   items would have as value the last letter of each one of the   characters between {}

My solution is as follows:

The array must initialize its index so that it is valid before PHP. Then my code tried to concatenate to a nonexistent index. Then the correct code is:

$patron = '/{usuario}/photos/{photo_id}/comments/{comment_id}/{sub}/data-long/{code_pek}';

$array_patron = str_split($patron);
$array3 = array();
$variable_open = false;
$ind = -1;
foreach($array_patron as $i => $val){
    if($val === '{'){
        $variable_open = true;
        $ind++;
        $array3[$ind] = ''; // Aquí está la solución
        continue;
    }else if($val === '}'){
        $variable_open = false;
        continue;
    }else if($variable_open){
        $array3[$ind] .= $val;
    }
}
print_r($array3);

Since the index is initialized, it can lend itself to concatenations without errors or notice s.

Result in:

Array (
    [0] => usuario
    [1] => photo_id
    [2] => comment_id
    [3] => sub
    [4] => code_pek
) 

And it is flexible to add more items between {} in the variable $ array_patron

I hope it can also serve you.

    
answered by 09.08.2017 / 22:49
source
2

Code

<?php
$patron = '/{usuario}/photos/{photo_id}/comments/{comment_id}';

$array_patron = str_split($patron);
$array3 = array("","","");
$variable_open = false;
$ind = -1;

foreach($array_patron as $i => $val){

    if($val === '{'){
        $variable_open = true;
        $ind++;
        continue;
    }else if($val === '}'){
        $variable_open = false;
        continue;

    }else if($variable_open){
        $array3[$ind] .= $val;  // Line number 22
    }

}
print_r($array3);
?>

Explanation

The warning that is thrown at you occurs when this line is executed:

$array3[$ind] .= $val;

What you are doing is indicating something like:

  

Concatenate to the current value of $array3[$ind] the value of the variable $val .

What happens is that in the first iteration (when $ind is 0,1,2) , for each of the indexes (0,1,2) $array3[$ind] does not contain nothing.

Therefore, being an empty index in the first iteration, you will get the error Undefined offset . Since there is no value with whom to concatenate. Obviously this only happens in the first iteration for each index, and that's why it only comes out 3 times.

Solution

You can change the value of $array3 for this.

$array3 = array("","","");

In that case, you leave each of the indices with an initial value, leaving in a certain way "initialized" each of its indices (0,1,2), for when you do the concatenation with the operator .= this will have what value to add in the concatenation.

sample line!

    
answered by 09.08.2017 в 22:31