How to transform more than one vector into an array using the php language?

Asked

Viewed 199 times

2

I have a file matriz_caminhos.txt with 20 rows and 20 columns, I need to access it and take the data as an array.

So far, I just got it to scan the file and return me 20 vectors.

Follows the code: .

<?php

            $ponteiro1 = fopen("C:/xampp/htdocs/www/grafoai/inputs/matriz_caminhos.txt","r");

              while (!feof($ponteiro1)) {

                  $linha1 = fgets($ponteiro1, 4096);     
                  //var_dump($linha1);      

                  $caminhos1 = array_map('trim', explode(",", $linha1)); //cidades = vetor

                  $vetorCaminhos = array();                    

                  foreach ($caminhos1 as $key1 => $value1) {

                    //Array
                    $vetorCaminhos = str_split($value1);
                    echo "<br><br>";
                    print_r($vetorCaminhos);                                    

                   } //Fim do Foreach      


            } //Fim do While

            fclose($ponteiro1)          

        ?>

The file "matriz_paths.txt" contains the following data:

01010000000000000000
10100000000000000000
01010000000000000000
00100100000000000000
00001010000000000000
00000101000000000000
00000010100000000000
00000001010001000000
00010000100001000000
10100000011000000000
00010000000100000000
00000000110100000000
00000000001011100000
00000000000100000000
00000000000100010100
00000000000000101000
00000000000000010000
00000000000000100000
00000000000000000101
00000000000000000010

I am using the following map as an example. Each number 1 that is in the txt file represents the position of the city on the map and its links (In order to know, eventually, which is the best way to go).

inserir a descrição da imagem aqui

I was trying to take the data as vector and then turn it into matrix. There is some possibility of already take this data as matrix? If so, what? If not, is there any possibility of turn these 20 vectors into a matrix?

Just for example, when I chose as origin the city "Arad" and destination city as "Eforie" and chose the Amplitude method. I must use a chained list to find the best way possible by the amplitude method.

After I select city of origin, destination and method, I with the code above, I was able to print the 20 vectors, as shown in the image down below:

inserir a descrição da imagem aqui

  • 1

    This is a graph problem, not chained list, as it is on the tag

  • In the teacher’s explanation, from what I understood, I should use a chained list to store the data to do the search.

  • 2

    Yes graphs have two most popular implementations, matrix and adjacency list. Chained list uses pointers to connect instantiated structures. http://www.professeurs.polymtl.ca/michel.gagnon/Disciplinas/Bac/Grafos/RepImpl/rep_impl.html

1 answer

2


I don’t understand why you treat the different first line. Here’s an example that generates a 4x4 matrix, you can adapt to your case:

$str = "0010\n1011\n0000\n0101";
$matriz = [];
$linhas = explode("\n", $str);

foreach ($linhas as $linha) {
    $colunas = str_split($linha);
    $matriz[] = $colunas;
}

print_r($matriz);
  • Oh yes... so, I’m starting with php... Dai was doing some tests to see how it works and everything else. Thank you, I’ll adapt here.

Browser other questions tagged

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