Search code examples
phparraysforeachdelimiterexplode

Can't set value after using explode()


I can't set value in the variables $nombre, $apellidos, $genero, $fecha_nacimiento, $ruta_obtenida and these errors are displayed:

Undefined offset: 1
Undefined offset: 2
Undefined offset: 3
Undefined offset: 4

When I print the values with echo the values are correctly displayed, but when I assign them to the variables, it does not work. Why is this happening?

$nombre = $apellidos = $genero = $clave1 = $clave2 = $fecha = $ruta = "";

$usuarioModel = new perfildatosModelo($_SESSION['el_correo']); 
$a_users = $usuarioModel->get_usuario_info($_SESSION['el_correo']); 
$count = 0; 

$pieces = explode("#", $a_users); 

foreach($pieces as $element): 
    $pieces = explode("|", $element);   
    $count++;  
    $nombre=$pieces[1]; 
    $apellidos=$pieces[2]; 
    $genero=$pieces[3]; 
    $fecha_nacimiento=$pieces[4];         
    $ruta_obtenida=$pieces[0];  
endforeach;

Solution

  • Your loop (while I personally wouldn't be reusing the $pieces variable name inside the loop) works as intended. I have a strong suspicion that you have a leading or trailing # that is messing with your process. (I wouldn't have to guess if you provided some sample input) Look at this demonstration:

    $a_users='#ro2|no2|ap2|ge2|fn2#';
    
    // A potential problem with exploding on a string with a leading or trailing #
    $pieces = explode("#", $a_users);  // first element is empty string, second holds values, third is empty string
    
    foreach($pieces as $element){
        $pieces = explode("|", $element);
        $nombre=$pieces[1]; 
        $apellidos=$pieces[2]; 
        $genero=$pieces[3]; 
        $fecha_nacimiento=$pieces[4];         
        $ruta_obtenida=$pieces[0];
        echo "nombre = $nombre\n";
        echo "apellidos = $apellidos\n";
        echo "genero = $genero\n";
        echo "fecha_nacimiento = $fecha_nacimiento\n";
        echo "ruta_obtenida = $ruta_obtenida\n\n";
    }
    

    This throws two batches of the same Undefined offset Notices that you are experiencing because of the leading and trailing #. To fix this, you can use something like this: $a_users=trim($a_users,'#'); before exploding on #.


    While your method will work on properly #delimited strings, a better / more concise approach would be to avoid the loop altogether and define your variables with list().

    If there is any danger of the pieces data not being complete, include a check that your $pieces string has enough pipe characters to deliver the expected number of values for your set of variables. Otherwise, you can omit the condition and move directly to list().

    Code: (Demo)

    $a_users='ro1|no1|ap1|ge1#ro2|no2|ap2|ge2|fn2';
    $valid_pieces_count=0;
    foreach(explode('#',$a_users) as $pieces){
        if(substr_count($pieces,'|')!=4){
            echo "Something went wrong, insufficient components in $pieces\n\n";
        }else{
            list($ruta_obtenida,$nombre,$apellidos,$genero,$fecha_nacimiento)=explode('|',$pieces);
            echo "nombre = $nombre\n";
            echo "apellidos = $apellidos\n";
            echo "genero = $genero\n";
            echo "fecha_nacimiento = $fecha_nacimiento\n";
            echo "ruta_obtenida = $ruta_obtenida\n\n";
            ++$valid_pieces_count;
        }
    }
    echo "valid_pieces_count = $valid_pieces_count";
    

    Output:

    Something went wrong, insufficient components in ro1|no1|ap1|ge1
    
    nombre = no2
    apellidos = ap2
    genero = ge2
    fecha_nacimiento = fn2
    ruta_obtenida = ro2
    
    valid_pieces_count = 1