2011-02-02 22 views
1
$links = array('https://google.com', 'http://aloe.com', 'http://foobar.org/image.jpg'); 

    foreach ($links as $link) 
    { 
      $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png'); 
      foreach ($unacceptables as $unacceptable) 
      { 
       if (strpos($link, $unacceptable) !== false) 
       { 
         echo 'not acceptable!<br />'; 
       } 
       else 
       { 
         echo 'acceptable<br />'; 
       } 
      } 
    } 

上記出力すべき:この単純なPHP式はなぜ機能しないのですか?

not acceptable 
acceptable 
not acceptable 

しかし、その代わりに出力する場合は、この混乱:それは右の仕事を得るための方法

not acceptable! 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
acceptable 
not acceptable! 
acceptable 
acceptable 
acceptable 
acceptable 

+2

あなたの場合は、他の*すべての*ごと* '$のunacceptable' *のために実行されています'$ link'です。 – BoltClock

+1

3つのリンクがあり、最初のforeachは3回実行されます。受け入れられないものが8つあり、2回目のforeachは8回実行されます。すべての実行で内部ループエコーが「許容」または「許容されない」。 3 * 8 = 24行の出力。 –

答えて

1

ループ内にループがあるので(それが8 * 3 = 24回出力される理由です) 変数$ is_acceptedを導入し、変数を内側ループの内側に設定し、内側ループの内側ではなく内側に答えを出力する必要があります。 (!直積)

あなたは、リソースごとに1つだけの出力を持ちたい、といないリソースと容認できないにつき1
$links = array('https://google.com', 'http://aloe.com', 'http://foobar.org/image.jpg'); 

foreach ($links as $link) 
{ 
     $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png'); 
    $is_accepted = true; 
     foreach ($unacceptables as $unacceptable) 
     { 
      if (strpos($link, $unacceptable) !== false) 
      { 
        $is_accepted = false; 
      } 
     } 

    if (!$is_accepted) 
     { 
      echo 'not acceptable!<br />'; 
     } 
     else 
     { 
      echo 'acceptable<br />'; 
     } 

} 
1

この試してみてください。

$isAcceptable = true; 
foreach ($unacceptables as $unaccetable) 
{ 
    if (strpos($link, $unacceptable) !== false) 
    { 
     $isAcceptable = false; 
     break; // not acceptable, no more checks needed 
    } 
} 
echo ($isAcceptable ? 'acceptable' : 'not acceptable'); 

代わりにforeachループのを。あなたが書いたコードのほとんどを維持しながら、ここで

0
$links = array('https://google.com', 'http://aloe.com', 'http://foobar.org/image.jpg'); 

foreach ($links as $link) 
{ 
     $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png'); 
     $accept = true; 
     foreach ($unacceptables as $unacceptable) 
     { 
      if (strpos($link, $unacceptable) !== false) 
      { 
        $accept = false; 
      } 
     } 

     if ($accept == true) { 
      echo "Acceptable<br />"; 
     } else { 
      echo "Not acceptable<br />"; 
     } 
} 
0

は、あなたのコードの修正です:

$links = array('https://google.com', 'http://aloe.com', 'http://foobar.org/image.jpg'); 

foreach ($links as $link) 
{ 
     $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png'); 
     $link_is_acceptable = true; 
     foreach ($unacceptables as $unacceptable) 
     { 
      if (strpos($link, $unacceptable) !== false){ 
       $link_is_acceptable = false; 
       break; 
      } 
     } 

     if ($link_is_acceptable) 
     { 
       echo 'acceptable<br />'; 
     } 
     else 
     { 
       echo 'not acceptable!<br />'; 
     } 
} 
関連する問題