Döngü baştan gitmiyor devam

2 Cevap php

I am having a bit of trouble understanding this bug. Here is the code:

 function filter_optionsfordash(array $data,$dash) {
    $filtered_options = array();
    $len = strlen($dash);    
    foreach ($data as $k => $value) {
      if (substr($k,0,$len) === $dash) {
        $option_name = trim(str_replace($dash . '_','',$k)); 

        switch ($option_name) {
          case 'displayColumns':
            $value = explode(',',$value);
          break;

          case 'dashletTitle':
            $option_name = 'title';
          break;

          case 'id':
          case 'module':
          case 'action':
          case 'configure':
          case 'to_pdf':
            continue;
          break;
        }


        $filtered_options[$option_name] = $value;         
      }
    }    

    return $filtered_options;
  }

Ne burada yapmaya çalışıyorum ben verilen adı ile başlamak vermek diziden bazı değerleri (bu durumda $_POST olacaktır) ($dash) filtre, ama 'id', 'modülü', 'eylem', 'Yapılandırma' veya 'to_pdf' olanları filtrelemek istiyorum.

Peki ne işe yarayacağını düşündüm bir 'devam' dir. Switch deyimi bir döngü olmadığından, 'devam' döngü (foreach) başlangıcına gitmek gerekir, ama görünüşe göre bu olmaz. Ben hala dizide istemiyorum anahtar adları alıyorum.

Ben kodu biraz değiştirerek, bir çözüm bulduk, ama ben gerçekten bu işe yaramazsa neden anlamak istiyorum.

'Devam' foreach beni geri göndermek gerekir!

2 Cevap

http://docs.php.net/continue diyor ki:

continue is used within looping structures to skip the rest of the current loop iteration [...]
Note: Note that in PHP the switch statement is considered a looping structure for the purposes of continue.

Bunun yerine 2 devam kullanmanız gerekir:

 function filter_optionsfordash(array $data,$dash) {
    $filtered_options = array();
    $len = strlen($dash);    
    foreach ($data as $k => $value) {
      if (substr($k,0,$len) === $dash) {
        $option_name = trim(str_replace($dash . '_','',$k)); 

        switch ($option_name) {
          case 'displayColumns':
            $value = explode(',',$value);
          break;

          case 'dashletTitle':
            $option_name = 'title';
          break;

          case 'id':
          case 'module':
          case 'action':
          case 'configure':
          case 'to_pdf':
            continue 2;
          break;
        }


        $filtered_options[$option_name] = $value;         
      }
    }    

    return $filtered_options;
  }