how to get an array of a file (file_get_contens) PHP?

0

I have a file user.txt where I keep this data:

Array ( [id_usuario] => 1 [0] => 1 [nombre_usuario] => superusuario [1] => superusuario [apellido_usuario] => Administrador [2] => Administrador [correo_usuario] => 131d2f680a7430382e755f8441342e [3] => 131d2f680a7430382e755f8441342e [contrasena] => 131d2f680a40 [4] => 131d2f680a40 [cedula_usuario] => 61 [5] => 61 [oficina_id] => 33 [6] => 33 [tipo_usuario_id] => 1 [7] => 1 [telefono_usuario] => [8] => [usuario_activado] => 1 [9] => 1 [fecha_reg] => 2017-12-01 00:00:00 [10] => 2017-12-01 00:00:00 )

Is there a way to transform this text into an array ?, or do I have to save each data in a separate file?

    
asked by Angel Zambrano 28.01.2018 в 03:26
source

1 answer

3

You can save it as a JSON and when reading it decifraz, like this:

$array = array(
    'id_usuario' => '1',
    '0' => '1',
    'nombre_usuario' => 'superusuario',
    '1' => 'superusuario',
    'apellido_usuario' => 'Administrador',
    '2' => 'Administrador',
    'correo_usuario' => '131d2f680a7430382e755f8441342e',
    '3' => '131d2f680a7430382e755f8441342e',
    'contrasena' => '131d2f680a40',
    '4' => '131d2f680a40',
    'cedula_usuario' => '61',
    '5' => '61',
    'oficina_id' => '33',
    '6' => '33',
    'tipo_usuario_id' => '1',
    '7' => '1',
    'telefono_usuario' => '',
    '8' => '',
    'usuario_activado' => '1',
    '9' => '1',
    'fecha_reg' => '2017-12-01 00:00:00',
    '10' => '2017-12-01 00:00:00',
);

file_put_contents('user.txt', json_encode($array));

$fileContent = file_get_contents('user.txt');

echo "<pre>";
print_r(json_decode($fileContent, true)); 
echo "</pre>";

It will print this to you, which is your% original% co

Array
(
    [id_usuario] => 1
    [0] => 1
    [nombre_usuario] => superusuario
    [1] => superusuario
    [apellido_usuario] => Administrador
    [2] => Administrador
    [correo_usuario] => 131d2f680a7430382e755f8441342e
    [3] => 131d2f680a7430382e755f8441342e
    [contrasena] => 131d2f680a40
    [4] => 131d2f680a40
    [cedula_usuario] => 61
    [5] => 61
    [oficina_id] => 33
    [6] => 33
    [tipo_usuario_id] => 1
    [7] => 1
    [telefono_usuario] => 
    [8] => 
    [usuario_activado] => 1
    [9] => 1
    [fecha_reg] => 2017-12-01 00:00:00
    [10] => 2017-12-01 00:00:00
)
    
answered by 28.01.2018 в 03:47