简体   繁体   中英

How to rewrite python program: foreach of two-dimensional array

people. I have a big problem here :hehe

I'm trying to rewrite code from python to php, but I'm stuck in the middle of this thing.

I'm creating algorithm called "Graphs | Depth first search"

Python code is fully working, PHP not.

balti = gretimumosarasas   
pilki = []                    
juodi = []


balti.insert(0,0)      

v0 = int(input("Insert first point: "))   
vg = int(input("Insert last point: "))  
print ("")
#Depth first search

for i in balti[v0]:         
    pilki.append(i)

juodi.append(v0)            
print("pilki liko", pilki) 
while (len(pilki) != 0) :   
    for x in pilki[:1]:     
        print ("tikrinu", x)    
        if vg == x:            
            print("yra!")       
            print("Kelias tarp ", v0, " ir ", vg, " egzistuoja!")
            pilki = []          
        else:                   
            for i in balti[x]:  
                if i not in pilki:  
                    if i not in juodi: 
                        pilki.append(i)
                        print("pridedu", i) 
            pilki.remove(x)     
            print ("pilki liko", (pilki))   
            juodi.append(x)     
            if len(pilki) == 0: 
                print ("Nera")  
                print("Kelias tarp ", v0, " ir ", vg, " neegzistuoja!")

gretimumosarasas is:

[[2, 3], [1, 4, 5], [1, 6], [2], [2], [3], []]

I'm stuck in this place

for i in balti[x]:  
            if i not in pilki:  
                if i not in juodi:
                    pilki.append(i) 
                    print("pridedu", i)

Don't know how to make two dimensional array foreach loop with index, because always getting notices, errors or forever loop.

My code of this program to PHP is:

$balti = $gretimumosarasas;
$pilki = array();
$juodi = array();
array_unshift($balti, '0');
?>
<form action="" method="post">
  Pradinė viršūnė: <input name="v0" type="text" />
  Galutinė viršūnė: <input name="vg" type="text" />
  <input name="submit" type="submit" value="Vykdyti"/>
</form>
<?php
if (isset($_POST['submit'])) {
    $v0 = $_POST['v0'];
    $vg = $_POST['vg'];

  foreach ($balti[$v0] as &$value) {
    array_push($pilki, $value);
  }
    echo"</br>";
    array_push($juodi, $v0);
    echo "Pilki liko: ";
    foreach ($pilki as $key => $val) {
    echo $val." ";
    }
    echo "</br>";
    $output = array_slice($pilki, 0, 1);
    while(count($pilki) != 0) {
        foreach ($output as $key => $value){
            echo "Tikrinu: ".$value."</br>";
            if ($vg == $value){
                echo "Yra!";
                print "Kelias tarp ".$v0." ir ".$vg." egzistuoja!";
                unset($pilki);
                $pilki=array();
            } else {
                foreach($balti as $arr){
                    if (!in_array($arr[$x], $pilki)) {
                        if (!in_array($arr[$x], $juodi)){
                            array_push($pilki, $arr[$x]);
                            echo "pridedu".$arr[$x];
                        }
                    }
                }



                $key = array_search($pilki[$x], $pilki);
                unset($pilki[$key]);

                echo "Pilki liko: ";
                foreach ($pilki as $key => $val) {
                    echo $val." ";
                }
                array_push($juodi, $pilki[$key]);
                if (count($pilki) == 0){
                    echo "Nėra!";
                    print "Kelias tarp ".$v0." ir ".$vg." neegzistuoja!";
                }
            }
        }
    }
}

echo "</br></br></br>";
print_r($juodi);

But it's not working correctly. Please give me a tips where I'm wrong and help to fix this code.

Proper output would be:

[0, [2, 3], [1, 4, 5], [1, 6], [2], [2], [3], []]
pilki liko [2, 3]
tikrinu 2
pridedu 4
pridedu 5
pilki liko [3, 4, 5]
tikrinu 3
pridedu 6
pilki liko [4, 5, 6]
tikrinu 4
pilki liko [5, 6]
tikrinu 5
pilki liko [6]
tikrinu 6
yra!
Kelias tarp  1  ir  6  egzistuoja!

With big respect, Justas.

The main error was in this part :-

foreach($balti as $arr){
    if (!in_array($arr[$x], $pilki)) {
        if (!in_array($arr[$x], $juodi)){
            array_push($pilki, $arr[$x]);
            echo "pridedu".$arr[$x];
        }
    }
}

$key = array_search($pilki[$x], $pilki);
unset($pilki[$key]);

echo "Pilki liko: ";
foreach ($pilki as $key => $val) {
    echo $val." ";
}
array_push($juodi, $pilki[$key]);
if (count($pilki) == 0){
    echo "Nėra!";
    print "Kelias tarp ".$v0." ir ".$vg." neegzistuoja!";
}
  • You used an undefined variable $x .

  • You were also traversing the whole $balti array instead of [ $value ] index/key of it ie $balti .

  • After popping out $pilki[key] , $pilki loses an element of the array, so its size reduces.

  • In that case, if you refer to $pilki[key] after popping, then it will point to another element of the array.
  • So, you might have to store $pilki[key] in a temporary variable and then append/insert it in $juodi array

This should work just fine !!! ( It took some effort phew! 😅)

<?php
    $balti = array(
        array(2, 3),
        array(1, 4 ,5),
        array(1, 6),
        array(2),
        array(2),
        array(3),
        array(),
    );
    $pilki = array();
    $juodi = array();
    array_unshift($balti, 0);
?>
<form action="index.php" method="post">
  Pradinė viršūnė: <input name="v0" type="text" />
  Galutinė viršūnė: <input name="vg" type="text" />
  <input name="submit" type="submit" value="Vykdyti"/>
</form>
<?php
if (isset($_POST['submit'])) {
    $v0 = $_POST['v0'];
    $vg = $_POST['vg'];

  foreach ($balti[$v0] as $value) {
    array_push($pilki, $value);
  }
    echo"</br>";
    array_push($juodi, $v0);
    echo "Pilki liko: ";
    foreach ($pilki as $val) {
    echo $val." ";
    }
    echo "</br>";
    while(count($pilki) != 0) {
        $output = array_slice($pilki, 0, 1);
        foreach ($output as $key => $value){
            echo "Tikrinu: ".$value."</br>";
            if ($vg == $value){
                echo "Yra!";
                print "Kelias tarp ".$v0." ir ".$vg." egzistuoja!";
                unset($pilki);
                $pilki=array();
            }
            else {
                foreach($balti[$value] as $x){
                    if (!in_array($x, $pilki)) {
                        if (!in_array($x, $juodi)){
                            array_push($pilki, $x);
                            echo "pridedu ".$x."<br>";
                        }
                    }
                }



                $key = array_search($value, $pilki);
                $pilkitemp = $pilki[$key];
                unset($pilki[$key]);

                echo "Pilki liko: ";
                foreach ($pilki as $key => $val) {
                    echo $val." ";
                }
                echo "<br>";
                array_push($juodi, $pilkitemp);
                if (count($pilki) == 0){
                    echo "Nėra!";
                    print "Kelias tarp ".$v0." ir ".$vg." neegzistuoja!";
                }
            }
        }
    }
}

echo "</br></br></br>";
print_r($juodi);
?>

Output :- 输出图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM