You can do it this way:
First open the contents of the text and save 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 transforms the division of :
in a array
of 2 items. Then we will use array_combine
to match keys with both 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 function file
:
$array = array_filter(file('arquivo.txt'));
In that case, the function file
already opens the file, leaving each line in an element of the array
.
To scroll through the values, just use the foreach
:
foreach ($array as $key => $value) {
echo $value['descricao'];
echo $value['nome'];
}
See an example on Ideone
How I even get the value?
– Amadeu Antunes
@Amadeuantunes if it is in a TXT file, the line with
file_get_contents
opens the desired file– Wallace Maxters
http://pcrinteriores.com/ is not giving
– Amadeu Antunes
Send the TXT so I can see
– Wallace Maxters
Art Deco.jpg:Description test! Taurus.jpg:Description test! 2
– Amadeu Antunes
http://pcrinteriores.com/img/portfolio/imagens.txt
– Amadeu Antunes
@Amadeuantunes but where is the header? In question you put header... If you want I change the answer
– Wallace Maxters
@Amadeuantunes now yes, it was completely different than I thought
– Wallace Maxters
Let’s go continue this discussion in chat.
– Amadeu Antunes