2016-03-21 12 views
-4

この関数は、すべてのシングルとダブルスの番号をエコーし​​ます。 (例えば01-01-02)。PHP各配列カテゴリの前にテキスト(タイトル)を置きます。

下記のコードを使用してそれぞれの 'カテゴリ'の前にタイトルを配置しようとしていますが、正しく動作しません。

この

SINGLES

01,02,03

ダブルス

01 02、03 04、05 06

あなたは私が修正助けることができるようにそれが何かする必要がありますこの?ありがとう!

function check_stats($todas){ 

    $arrlength = count($todas); 



    for($x = 0; $x < $arrlength; $x++) { 


     $arrlength2 = count($todas[$x]); 

     if($arrlength2==1){ 

      echo 'single'.'</br></br>'; 

      list($a) = explode(" ", implode(" ", $todas[$x])); 

      echo $a; echo '</br>'; 

     } 

     if($arrlength2==2){ 

      echo 'double'.'</br></br>'; 

      list($a,$b) = explode(" ", implode(" ", $todas[$x])); 

      echo $a.' '.$b; echo '</br>'; 

     } 

    } //for 

} //function 
+0

カテゴリはどこですか? –

+0

ありがとう@AliFarhoudi。各IF条件の後である。 ( 'single'と 'double'として) – Tom

+0

質問を編集しました。ありがとう@AliFarhoudi – Tom

答えて

0

あなたはシングルとダブルを分けようとしています。そしてそれらを別々に印刷したいとします。それらをすべて検出していない間は、それらを個別に印刷することはできません。次に、すべてのカテゴリを検出する必要があり、その後でそれらを個別に印刷することができます。これを行うには、検出時にカテゴリを保存し、検出後に保存されたカテゴリを印刷する必要があります。

function check_stats($todas){ 
    $singles = array(); // an array to save single numbers 
    $doubles = array(); // an array so save doubles 

    foreach ($todas as $toda) { // cleaner and beautifier than for 
     if (count($toda) == 1) { // direct compare without extra $arrlength2 
      $singles[] = $toda[0]; // will push $toda[0] to $singles array 
     } else if (count($toda) == 2) { // when put else compares less 
      $doubles[] = $toda[0] . ' ' . $toda[1]; 
     } 
    } 

    // then you can do whatever with $singles and $doubles 

    echo 'SINGLES' . PHP_EOL; 
    foreach ($singles as $single) { 
     echo $single . PHP_EOL; 
    } 

    echo 'DOUBLES' . PHP_EOL; 
    foreach ($doubles as $double) { 
     echo $double . PHP_EOL; 
    } 
} 

編集#1: 変数以上の2種類があった場合は、アレイ内でそれらを保つことができます。

function check_stats($todas){ 
    $result_array = array(); 

    foreach ($todas as $toda) { 
     $result_array[count($toda)] = implode(' ', $toda); 
     // the above code will save each single or double or triple or etc 
     // in an index in their size 
    } 

    return $result_array; // better return it and use out of function 
} 

$result = check_stats($todas); 
// now print them 
foreach ($todas as $key => $toda) { 
    echo $key . PHP_EOL; // print count (that is index) 
    foreach ($toda as $t) { 
     echo $t . PHP_EOL; 
    } 
} 
+0

ありがとう!しかし、シングル、ダブル、トリプルが無限にあればどうなりますか? – Tom

+0

という意味です。シングルやダブルス以上にすることができます。 – Tom

+0

ありがとう!できます! – Tom

関連する問題