Read array from a file in PHP with Laravel

0

I have an array saved in a file inside a folder that I created in the App called Arrays. It is the following:

$contentTypeArray=array(
    1  => 'TEXTO',
    2 => 'PDF',
    3 => 'PPT',
    4 => 'AUDIO',
    5 => 'VIDEO',
    6 => 'HTML',
    );

How can I access that array? I know how to use Collections but I do not know how to access the document either.

    
asked by Angel Prieto 12.09.2017 в 10:23
source

1 answer

1

Your problem has more than 2 possible solutions. 1 create a file where you put the fix, name it as a php file for example fix.php, position it in some directory of your project and include it where you are going to use it with an include_once instruction 'path_to_file'

The 2nd option is to save the array data as a json in a file and then read the file, and decode the json.

In the first option, your fix file would look like:

<?php
$contentTypeArray=array(
1  => 'TEXTO',
2 => 'PDF',
3 => 'PPT',
4 => 'AUDIO',
5 => 'VIDEO',
6 => 'HTML',
);

And where you want to use it, you just have to put it at the beginning of the file

 include_once 'ruta_al_fichero.php';

and you can use it as if you had declared it in this same file, with the same variable name

The second option would have to save your arrangement like a json

$fichero = fopen("ruta_donde_voy_a_guardar_el_archivo","w+");
$fichero->write($fichero, json_encode($mi_arreglo));
fclose($fichero);

Now if you want to read it you should load it from the route where you stored it

$ruta = "ruta_donde_guardastes_el_archivo";
$fichero = fopen($ruta,"r");
$contenido = fread($fichero,filesize($ruta));
//Ya en mi_arreglo tienes el arreglo original
$mi_arreglo = json_decode($contenido);
    
answered by 12.09.2017 / 16:32
source