0

我制作了一个代码,它为我提供了手动插入的输入的最短路径,但我需要在我的代码中使用我的数据库值 我现在的代码是:

 $g = new Graph();
$g->addedge("a", "b",2);

$g->addedge("a", "d", 1);

$g->addedge("b", "a", 74);
$g->addedge("b", "c", 2);
$g->addedge("b", "e", 12);

$g->addedge("c", "b", 12);
$g->addedge("c", "j", 12);
$g->addedge("c", "f", 74);

$g->addedge("d", "g", 22);
$g->addedge("d", "e", 32);

$g->addedge("e", "h", 33);
$g->addedge("e", "d", 66);
$g->addedge("e", "f", 76);

$g->addedge("f", "j", 21);
$g->addedge("f", "i", 11);

$g->addedge("g", "c", 12);
$g->addedge("g", "h", 10);

$g->addedge("h", "g", 2);
$g->addedge("h", "i", 72);

$g->addedge("i", "j", 7);
$g->addedge("i", "f", 31);
$g->addedge("i", "h", 18);

$g->addedge("j", "f", 8);



list($distances, $prev) = $g->paths_from('a');

$path = $g->paths_to($prev, 'i');

print_r($path);

我想用for循环来简化这段代码

$g = new Graph();
    for ($i=0; $i <9 ; $i++) {
        for ($j=0; $j <9 ; $j++) { 
        $g->addedge("a", "b",$row['from my database row']); 
 //a and b i use to increase the ASCII value according to i and j
        } 

    }

现在我如何根据 j 的值添加我的数据库表行任何人都可以帮助我如何制作这种简短形式的 php 代码???

4

0 回答 0