2015-10-08 30 views
0

如何停止執行else塊foreach循環內的數組中的項目數?這是可能的,還是應該改變程序邏輯?如何打破foreach循環來停止執行else塊內?

僞代碼這裏要明確什麼我想做的事:

input = 2 
array = [1,2,3,4,...] 
foreach item in array 
    if input equals 3 
     logs MATCH 
    else 
     logs NOT MATCH // logs once for 1, not for 2, logs for 3,4,number of items 
end foreach 

我曾嘗試與突破,但我沒有得到預期的效果,如果我打破別人,第一次運行foreach循環會停住了,在這種特殊情況下,數組中的第二項永遠不會被測試。

Actuall代碼是波紋管,但我希望僞代碼足夠清晰。

// take user input 
$phone_number = '004913535030'; 
// initialize countrycodes 
$countrycodes = [385,49,386]; 

// is number local number, one or zero zeros, two digit local or mobile code, and 6 or 7 digits? 
if (preg_match('(^0*\d{0,2}\d{6,7}$)', $phone_number)) 
{ 
    // If yes, then we deal with local number, without country code prefix 
    // Remove zeroes at begining if any, and add "+countrycode(385)." in front of clean number 
    echo "local number"; 
    echo 'Original number is: ' . $phone_number . '<br>'; 
    echo 'Country code is not matched!'; 
    echo '<br>' . 'Stripped number is: ' . preg_replace("/^(0+)/", '', $phone_number); 
    echo '<br>' . 'Formatted number is: ' . '+' . $countrycodes[0] . '.' . preg_replace("/^(0{1,}$countrycodes[0])|^($countrycodes[0])|^(0+)/",'',$phone_number); 
} 
else 
{ 
    // bla bla, for each item in array check match, then strip number, and format it according to EPP RFC standard 
    foreach($countrycodes as $countrycode) 
    { 
     // Do we have country code in phone number and number longer than 9 characters? Then some of EU members phone number 
     // Clean number, remove zeroes at begining if any, and add "+countrycode." in front of clean number 
     if (preg_match("/^(0{1,}$countrycode\d{8,})|^($countrycode\d{8,})/", $phone_number, $match[0])) 
     { 
      echo 'Original number is: ' . $phone_number . '<br>'; 
      echo 'Country code is matched ' . '<br>' . 'Country code is:' . $countrycode; 
      //print_r($match); 
      // strip country code and one or more preceding zeros 
      echo '<br>' . 'Stripped number is: ' . preg_replace("/^(0{1,}$countrycode)|^($countrycode)|^(0+)/", '', $phone_number); 
      echo '<br>' . 'Formatted number is: ' . '+' . $countrycode . '.' . preg_replace("/^(0{1,}$countrycode)|^($countrycode)|^(0+)/",'',$phone_number); 
      // break 
     } 
     // HOW TO PREVENT EXECUTING OF ELSE BLOCK NUMBER OF ITEMS IN COUNTRYCODES ARRAY TIMES BUT PRINT WHEN COUNTRYCODE NOT MATCHED? 
     else 
     { 
      echo "Not an EU number"; 
     } 
    } 
} 

回答

0

我想你想檢查電話號碼與所有的國家代碼,並打印結果一次,如果不匹配。我認爲你不需要其他條件。你應該試試這個:

$matched = false; 
foreach($countrycodes as $countrycode) 
    { 
     // Do we have country code in phone number and number longer than 9 characters? Then some of EU members phone number 
     // Clean number, remove zeroes at begining if any, and add "+countrycode." in front of clean number 
     if (preg_match("/^(0{1,}$countrycode\d{8,})|^($countrycode\d{8,})/", $phone_number, $match[0])) 
     { 
      echo 'Original number is: ' . $phone_number . '<br>'; 
      echo 'Country code is matched ' . '<br>' . 'Country code is:' . $countrycode; 
      //print_r($match); 
      // strip country code and one or more preceding zeros 
      echo '<br>' . 'Stripped number is: ' . preg_replace("/^(0{1,}$countrycode)|^($countrycode)|^(0+)/", '', $phone_number); 
      echo '<br>' . 'Formatted number is: ' . '+' . $countrycode . '.' . preg_replace("/^(0{1,}$countrycode)|^($countrycode)|^(0+)/",'',$phone_number); 
$matched = true;    
break; 
     } 

    } 
if (!$matched) { 
echo "Not an EU number"; 
} 
+0

輝煌。這正是我想要做的。 –

+0

很高興幫助你:) –

0

這並不完全清楚你在問什麼,但我會採取一個刺。

如果找到匹配項,您是否想要停止循環處理?然後,你要尋找的命令是break;

foreach($countrycodes as $countrycode) 
{ 
    if (preg_match("/^(0{1,}$countrycode\d{8,})|^($countrycode\d{8,})/", $phone_number, $match[0])) 
    { 
     echo 'Original number is: ' . $phone_number . '<br>'; 
     echo 'Country code is matched ' . '<br>' . 'Country code is:' . $countrycode; 
     echo '<br>' . 'Stripped number is: ' . preg_replace("/^(0{1,}$countrycode)|^($countrycode)|^(0+)/", '', $phone_number); 
     echo '<br>' . 'Formatted number is: ' . '+' . $countrycode . '.' . preg_replace("/^(0{1,}$countrycode)|^($countrycode)|^(0+)/",'',$phone_number); 
     break; 
    } 
    else 
    { 
     echo "Not an EU number"; 
    } 
} 

break;命令將停止處理循環。

+0

我已經嘗試過休息,但是foreach會運行第一個項目,並且在它命中匹配之後會中斷。也許我應該嘗試與匹配數或類似的東西。 –