Use file data in php code

Asked

Viewed 62 times

1

I would like to know how to modify such code so that it reads the file data .txt to carry out the search from them.

Code:

<?php

function dijkstra($graph_array, $source, $target) {
    $vertices = array();

    $neighbours = array();
    foreach ($graph_array as $edge) {
        array_push($vertices, $edge[0], $edge[1]);
        $neighbours[$edge[0]][] = array("end" => $edge[1], "cost" => $edge[2]);
        $neighbours[$edge[1]][] = array("end" => $edge[0], "cost" => $edge[2]);
    }
    $vertices = array_unique($vertices);
 echo $vertices[5]."<br>";
    foreach ($vertices as $vertex) {
        $dist[$vertex] = INF;
        $previous[$vertex] = NULL;
    }

    $dist[$source] = 0;
    $Q = $vertices;
    while (count($Q) > 0) {

        // TODO - Find faster way to get minimum
        $min = INF;
        foreach ($Q as $vertex){
            if ($dist[$vertex] < $min) {
                $min = $dist[$vertex];
                $u = $vertex;
                echo $u;
            }
        }

        $Q = array_diff($Q, array($u));
        if ($dist[$u] == INF or $u == $target) {
            break;
        }

        if (isset($neighbours[$u])) {
            foreach ($neighbours[$u] as $arr) {
                $alt = $dist[$u] + $arr["cost"];
                if ($alt < $dist[$arr["end"]]) {
                    $dist[$arr["end"]] = $alt;
                    $previous[$arr["end"]] = $u;
                }
            }
        }
    }
    $path = array();
    $u = $target;
    while (isset($previous[$u])) {
        array_unshift($path, $u);
        $u = $previous[$u];
    }
    array_unshift($path, $u);
    return $path;
}

$graph_array = array(
                    array("a", "b", 7),
                    array("a", "c", 9),
                    array("a", "f", 14),
                    array("b", "c", 10),
                    array("b", "d", 15),
                    array("c", "h", 11),
                    array("c", "f", 2),
                    array("d", "e", 6),
                    array("e", "f", 9),
                    array("f", "h", 7),
                    array("h", "i", 3)
               );

$path = dijkstra($graph_array, "a", "i");

echo "path is: ".implode(", ", $path)."\n";

?>

txt file:

city=A(100,80);
city=B(160,70);
city=C(110,50);
city=D(140,120);
city=F(155,40);
city=G(210,60);
city=H(190,10);
city=I(170,110);
route=A-C;140;
route=A-D;155;
route=C-F;125;
route=D-B;115;
route=D-I;152;
route=B-F;119;
route=B-G;136;
route=G-F;133;
route=F-H;163;
route=I-H;197;
  • 2

    It would be important [Dit] to post and show how tried to do, and what was the stage that had difficulty, so it makes it easier for those who answer.

  • From what I understand, you want to put the code file reading, that?

  • Yes, put the file read instead of typing what I want to search!

  • @Luizfelipe Do you want to pass only the "routes" that are in the file to this function? the "cities" not? something else, for example, on the last line you want to pick up I, H and 197 separately or the whole line?

  • @stderr I want both "routes" and "cities"! Since the values that are next to the cities and routes are to perform the calculation to determine the shortest distance!

  • @Luizfelipe Ok.. in the case of "cities", you want to take the letter and the two numbers separated by , right? and "routes" you want to get the letters separated by - and the number, correct?

  • @stderr That is correct !

  • 1

    @stderr I thought I had evaluated that day, I’m sorry! Yes, it worked the way I needed it! And again, I’m sorry I didn’t give you a feedback earlier.

Show 3 more comments

1 answer

1


To read the contents of the text file, you can use the file_get_contents:

$linhas = file_get_contents('mapa.txt');

To get the data you want, use the method preg_match_all:

preg_match_all("~(?:city|route)=(\w+)(?:\(|\-)(\w+)(?:\,|\;)(\w+)~", $linhas, $pontos);

The variable pontos shall contain the results obtained from the expression (?:city|route)=(\w+)(?:\(|\-)(\w+)(?:\,|\;)(\w+) which will correspond to letters and numbers followed by ( or -, letters or numbers followed by , or ; and letters and numbers followed by ) or ;. Test for expression here.

See DEMO

city=A(100,80);
city=B(160,70);
route=A-C;140;
route=A-D;155;

Assuming the file has the above data, the result will be:

  • $pontos[1]: A, B, A, A,
  • $pontos[2]: 100, 160, C, D
  • $pontos[3]: 80, 70, 140, 155

Browser other questions tagged

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