You can do it this way:
First open the text content and save it to a variable:
$string = file_get_contents('arquivo.txt');
Next we will create an array with each line of the file.
$array = array_filter(explode("\n", $string));
Now let's map the array
so that it turns the division of :
into a array
of 2 items. Then we will use array_combine
to combine keys with the two values (keys will be "name" and "description")
$callback = function ($value) {
return array_combine(array('nome', 'descricao'), explode(":", $value, 2));
};
$array = array_map($callback, $array);
Finally, we can see the result:
var_dump($array);
Improvements
Instead of using file_get_contents
and then a explode
in \n
, we can evolve in this expression using the file
function:
$array = array_filter(file('arquivo.txt'));
In this case, the file
function already opens the file, leaving each line in an element of array
.
To scroll through the values, simply use foreach
:
foreach ($array as $key => $value) {
echo $value['descricao'];
echo $value['nome'];
}
See an example at Ideone