duanrong5927 2015-10-08 21:55
浏览 119
已采纳

如何打破foreach循环以停止执行其他块内部?

How to stop executing else block number of items in array inside foreach loop? Is that possible, or should I change program logic?

Pseudocode is here to make clear what I want to do:

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

I have tried with break, but I don't get desired effect, if I break else, on first run foreach loop will be stoped, and in that particular case, second item in array never will be tested.

Actuall code is bellow, but I hope that pseudo code is clear enough.

// 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";
        }
    }
}
  • 写回答

2条回答 默认 最新

  • drgweamoi473182981 2015-10-08 22:19
    关注

    I think you want to check the phone number with all the country codes and print the result once if not matched. I think you don't need a else condition. You should try this:

    $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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)