建立一个有时可以连续的循环,有时不是

时间:2015-07-08 22:33:18

标签: php loops while-loop

您可以在阅读

后向我推荐标题

所以我有这个数据库

 name      id1    id2

 carl     1154    0
 mary     4592    0
 jake     5820    4592
 john     6495    0
 shannon  1047    6495
 kalsey   2281    5820
 scarlet  4419    2281

我要告诉我上面要做的事情,因为我的英语不好。如果你不明白,请通知我。

  • Carl的id2 = 0
  • mary'S id2 = 0
  • jake的id2!= 0 ---->就在那里我想在id1部分找到jake的id2。 (jack id2 = 4592,这是玛丽的id1)。现在我想再次得到玛丽的id2,这意味着我可以寻找下一个名字(如果玛丽的id2不等于0我想继续循环)
  • john的id2 = 0
  • shannon的id2 = 6495 ----> 6495是约翰的id1和约翰的id2 = 0 ---->循环结束
  • kalsey的id2 = 5820 -----> 5820给了我杰克---> jake的id2不等于0 agian(其4592)--->继续循环---> 4592给了我玛丽--->结婚的id2 = 0 --->循环结束
  • 猩红色的id2 = 2281 ---->把我们带到kalsey -----> kalsey把我们带到了杰克---->杰克带我们玛丽--->循环结束。

我将如何编写我的程序? 我尝试过这样的事情。

//first while gets id2
while($row = mysqli_fetch_array($command)){
    while ($row[id2] != 0){
        //get id1 
        // find id2
     }
}

当这个程序达到jake时,它会重复到无穷大------> carl,mary,jake,mary,jake,mary,jake,mary,jake,mary,jake,.....

2 个答案:

答案 0 :(得分:0)

这看起来很有趣,正如其他人在评论中所说,你需要递归,而不是常规循环。

<?php

function answer($data, $rowNumber) {
  // Echo so you can see the path
  echo $data[$rowNumber]['name'] . " --> ";

  // Get id2
  $id2 = $data[$rowNumber]['id2'];

  // If id2 is 0, then return success
  if ($id2 == 0) {
    echo "done\n";
    return true;
  }

  // If id2 isn't 0, look up the next name
  foreach ($data as $rowNum => $d) {
    if ($d['id1'] == $id2) {
      return answer($data, $rowNum);
    }
  }

  // No next name had id2=0, failure
  echo "failure, no path ends with id2=0\n";
  return false;
}

$array = array (
 array ('name' => 'mary',    'id1' => 4592, 'id2' => 0),
 array ('name' => 'jake',    'id1' => 5820, 'id2' => 4592),
 array ('name' => 'kalsey',  'id1' => 2281, 'id2' => 5820),
 array ('name' => 'scarlet', 'id1' => 4419, 'id2' => 2281),
);

answer($array, 3);

答案 1 :(得分:0)

我没有您的数据库,所以我使用的是数组,您只需复制粘贴并运行下一个代码,然后告诉我这是否是您的预期结果,它是递归的,所以&& #39;有点难以解释,但我会在你测试之后解释它:

<?php
echo "Right sequence : " .
     "carl-mary-jake-mary-john-shannon-john-kalsey-jake-mary-scarlet-kalsey-jake-mary" .
     "<br/>" .
     "New sequence : ";
$database = Array( Array( "name" => "carl",    "id1"  => "1154", "id2"  => "0"    ),
                   Array( "name" => "mary",    "id1"  => "4592", "id2"  => "0"    ),
                   Array( "name" => "jake",    "id1"  => "5820", "id2"  => "4592" ),
                   Array( "name" => "john",    "id1"  => "6495", "id2"  => "0"    ),
                   Array( "name" => "shannon", "id1"  => "1047", "id2"  => "6495" ),
                   Array( "name" => "kalsey",  "id1"  => "2281", "id2"  => "5820" ),
                   Array( "name" => "scarlet", "id1"  => "4419", "id2"  => "2281" )
                 );

recursive( 0,TRUE ); // START PROCESS.

// $I    : POINTS TO EACH ELEMENT IN ARRAY.
// $NEXT : TRUE  = JUMP TO NEXT ELEMENT.
//         FALSE = DON'T JUMP TO NEXT ELEMENT.
function recursive ( $i,$next )  {
global $database;
if ( $i < count( $database ) ) // IF NOT BEYOND LIMITS...
   { echo $database[ $i ][ "name" ] . "-";
     if ( $database[ $i ][ "id2" ] != "0" ) // IF HAS LINKED ELEMENT...
        recursive( find_id2( $database[ $i ][ "id2" ] ),false ); // FALSE = DONT VISIT NEXT ELEMENT.
     if ( $next )
        recursive( ++$i,true ); // TRUE = VISIT NEXT ELEMENT.
   }
}

function find_id2 ( $id2 ) {
global $database;
for ( $i = 0; $i < count( $database ); $i++ )
  if ( $database[ $i ][ "id1" ] == $id2 )
     return $i;
return -1; // NOT FOUND.
}

?>

参数$next具有魔力(&#34;有时可以是连续的,有时不是&#34;),由于它我们决定是否必须访问下一个元素。例如,&#34; carl&#34;必须访问下一个,#34; mary&#34; (真的),但是&#34;杰克&#34;跳到&#34; mary&#34;由于$next,我们知道&#34; mary&#34;一定不要跳到&#34;杰克&#34; (假)或我们将永远循环。