How to transform this text into an array with name and description?

Asked

Viewed 1,008 times

4

I have a.txt file. It has the name of the images and the description about them. So I wanted to do it this way:

nome:descrição
imagem1.jpg:"descrição exemplo 1";

I know how to read the php file. Now I wanted to separate the information, but I don’t know what function in can do this in order to have $nome and $descrição in different variables.

2 answers

3


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?

  • @Amadeuantunes if it is in a TXT file, the line with file_get_contents opens the desired file

  • http://pcrinteriores.com/ is not giving

  • Send the TXT so I can see

  • Art Deco.jpg:Description test! Taurus.jpg:Description test! 2

  • http://pcrinteriores.com/img/portfolio/imagens.txt

  • @Amadeuantunes but where is the header? In question you put header... If you want I change the answer

  • @Amadeuantunes now yes, it was completely different than I thought

Show 4 more comments

2

You can use the file():

$arquivo = file('arquivo.txt');

foreach($arquivo as $index => $linha){

    list($info[$index]['imagem'], $info[$index]['descricao']) = explode(':', trim($linha));

}

That way it will result in:

array(2) {
  [0]=>
  array(2) {
    ["imagem"]=>
    string(12) "Art Deco.jpg"
    ["descricao"]=>
    string(20) "Descrição teste!"
  }
  [1]=>
  array(2) {
    ["imagem"]=>
    string(9) "Touro.jpg"
    ["descricao"]=>
    string(19) "Descrição teste!2"
  }
}

So you can manipulate the array as you like, it’s not clear how or what the objectives are after reading the data, if you want to display them you can do:

foreach($arquivo as $linha){

    list($imagem, $descricao) = explode(':', trim($linha));

    echo '<img src="'.$imagem.'" />';
    echo '<p>'.$descricao.'</p>';

}
  • 1

    The idea of list($image, $descricao) is great, you don’t even need to allocate the values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.