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

2

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

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

Follow 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 "path_array.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'm using the map below as an example. Each number 1 that is in the txt file represents the position of the city on the map and its links. (To be able to eventually find out the best way to go).

  

Iwastryingtogetthedataasavectorandthen  matrix.Isthereanypossibilityofalreadytakingthisdataas  array?Ifso,whichone?Ifnot,isthereanypossibilityof  Turnthese20vectorsintoanarray?

    

Justforexemplification,whenIchoseastheoriginthecity"Arad"   and destination city as "Eforie" and chose Amplitude method. I must   use a linked list to search for the best path   possible through the amplitude method.

     

After I select source city, destination, and method, I with the code   above, I was able to print the 20 vectors, as the image shows.   below:

    
asked by anonymous 24.03.2018 / 15:00

1 answer

2

I do not understand why you treat the first line differently. Here is an example that generates a 4x4 array, 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);
    
24.03.2018 / 15:17