Create a repetition or route every certain data from an explode

3

I have the following code:

$pal = "3,5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555";
$pal = explode(" ",$pal);
print_r($pal);

The result is something like this:

Array
(
    [0] => 3,5
    [1] => 1401
    [2] => 4145
    [3] => 7854
    [4] => 8454
    [5] => 7458
    [6] => 5152556555
    [7] => 3,5
    [8] => 1401
    [9] => 4145
    [10] => 7854
    [11] => 8454
    [12] => 7458
    [13] => 5152556555
    [14] => 3,5
    [15] => 1401
    [16] => 4145
    [17] => 7854
    [18] => 8454
    [19] => 7458
    [20] => 5152556555
)

The data of the $ pal variable is entered from a textbox, its content is something like:

valor  Codigo                     serial
3,5    1401 4145 7854 8454 7458   5152556555
3,5    1401 4145 7854 8454 7458   5152556555
3,5    1401 4145 7854 8454 7458   5152556555

I would like the data to be stored in a database after doing the explode with the following logic:

     // He visto por alli algo como esto:
list($valor,$codigo,$serial) = explode(" ",$pal);
    $codigo =  $pal[1] .$pal[2] .$pal[3] .$pal[4] .$pal[5];
        echo $valor; // Imprime su valor
        echo $codigo; // Imprime el codigo
        echo $serial; // Imprime el serial 

But I do not know how to get the text block separated in the way I am requiring it.

    
asked by Jose M Herrera V 17.09.2018 в 19:17
source

3 answers

1

Since the length of each element can vary but they are always the same number of elements, the first option that you raise would not be valid, but you can do it in the following way.

<?php
$datos = "3,5 1401 4145 7854 8454 7458 5152556555 5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555";
// divides por espacios y cada 7 elementos, los elementos de cada fila
$temp = array_chunk(explode(' ', $datos), 7);
$ar = array();

foreach($temp as $key => $v) {
    // optienes el 1º elemento valor
    $ar[$key]['valor'] = array_shift($v);
    // optienes el ultimo elemento, serial
    $ar[$key]['serial'] = array_pop($v);
    // lo que queda es el codigo, lo unes con espacios
    $ar[$key]['codigo'] = implode(' ', $v);
}

echo '<pre>';
print_r($ar);
echo '</pre>';

And choose the same result:

Array
(
    [0] => Array
        (
            [valor] => 3,5
            [serial] => 5152556555
            [codigo] => 1401 4145 7854 8454 7458
        )

    [1] => Array
        (
            [valor] => 5
            [serial] => 5152556555
            [codigo] => 1401 4145 7854 8454 7458
        )

    [2] => Array
        (
            [valor] => 3,5
            [serial] => 5152556555
            [codigo] => 1401 4145 7854 8454 7458
        )

)

First option

If the data is always the same length you can cut the string to remove the fragments you need, example

<?php
$datos = "3,5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555";

$temp = str_split($datos, 40);// cortamos cada 40 caracteres
$ar = array();

// recorremos y vamos cortando cada dato individual
foreach($temp as $key => $value) {
    $ar[$key]['valor'] = substr($value, 0, 3);
    $ar[$key]['codigo'] = substr($value, 4, 24);
    $ar[$key]['serial'] = substr($value, 29, 11);
}


echo '<pre>';
print_r($ar);
echo '</pre>';

Result:

Array
(
    [0] => Array
        (
            [valor] => 3,5
            [codigo] => 1401 4145 7854 8454 7458
            [serial] => 5152556555 
        )

    [1] => Array
        (
            [valor] => 3,5
            [codigo] => 1401 4145 7854 8454 7458
            [serial] => 5152556555 
        )

    [2] => Array
        (
            [valor] => 3,5
            [codigo] => 1401 4145 7854 8454 7458
            [serial] => 5152556555
        )

)
    
answered by 17.09.2018 / 20:38
source
2

This other variant will be useful even if the codes, value or serial have variable length, as shown in the output.

<?php
function string_to_array($string) {
    $len = strlen($string);
    $spc_pos = array();
    for($i=0; $i<$len; $i++) {
        if($string[$i] === ' ') {
            $spc_pos[] = $i;
        }
    }
    $offset0 = $spc_pos[0];
    $subarray[] = substr($string, 0, $offset0); // Valor
    $offset1 = $spc_pos[5] - $spc_pos[0] - 1; // La longitud del segundo string
    $subarray[] = substr($string, $spc_pos[0]+1, $offset1); // Codigo
    $subarray[] = substr($string, $spc_pos[5]+1); // Serial
    return $subarray;
}

$pal = "3,5 1401 4145 7854 8454 7458 5152556555 42,8 1401 4145 7854 8454 74585 51525555 100,32 14001 88145 78548 8454 7458 5152556555532";

$len = strlen($pal);
$start = 0;
$final_array = array();
$num_space = 0;
for($i=0; $i<$len; $i++) {
    if($pal[$i] == ' ') {
        $num_space++;
    }
    if($i == $len-1) { // Si llego al final del string
        $i++; // Para que tome el ultimo elemento
        $num_space++; // Como al final no hay espacio, se cuenta uno adicional.
    }
    if(($num_space == 7)) {
        $substring = substr($pal, $start, $i-$start);
        $final_array[] = string_to_array($substring);
        $num_space = 0;
        $start = $i+1;
    }
}

print_r($final_array);

Exit

Array
(
    [0] => Array
        (
            [0] => 3,5
            [1] => 1401 4145 7854 8454 7458
            [2] => 5152556555
        )

    [1] => Array
        (
            [0] => 42,8
            [1] => 1401 4145 7854 8454 74585
            [2] => 51525555
        )

    [2] => Array
        (
            [0] => 100,32
            [1] => 14001 88145 78548 8454 7458
            [2] => 5152556555532
        )

)
    
answered by 17.09.2018 в 20:56
0

This is a somewhat rudimentary way of operating with what you have. By not being completely clear what you need I have created a structure that serves as a starting point to do what you want:

PHP

$pal = "3,5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555 3,5 1401 4145 7854 8454 7458 5152556555";
$pal = explode(" ",$pal);

$ciclo = 0;

$seriales = '';
$valores = '';
$codigos = '';

for ($i = 0; $i < count($pal); $i++) {

    if ($ciclo == 0) {
        $valores .= $pal[$i] . ' ';
    }
    else {
        if ($ciclo == 6) {
            $seriales .= $pal[$i] . ' ';
            $ciclo = -1;
        }
        else {
            $codigos .= $pal[$i] . ' ';
        }
    }

    $ciclo++;

}


echo 'Seriales son: ' . $seriales;
echo ' Valores son: ' . $valores;
echo ' Codigos son: ' . $codigos;

Basically, I do it is to create a cycle of 6 rotations, the first rotation of each cycle would correspond to your values , the last one to your serials and the rest would be your codes .

    
answered by 17.09.2018 в 20:36