2016-03-21 11 views
-4

Bu işlev tüm bekar ve çift sayıları yankılamak zorundadır. (örneğin 01 - 01 02).PHP Metin dizisini (başlık) her dizi kategorisinden önce yerleştirin

Aşağıdaki kodu kullanarak her 'kategori''den önce bir başlık yerleştirmeye çalışıyorum ve düzgün çalışmıyor.

O 05 06

Bana düzeltmek yardım edebilir ÇİFTLER

01 02, 03 04, bu

SINGLES

01,02,03

gibi bir şey olmalı bu? Teşekkürler!

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

Kategori nerededir? –

+0

Teşekkürler @AliFarhoudi. Her bir IF koşulundan sonra. ('bekar' ve 'çift' olarak) – Tom

+0

Soruyu yeni düzenledim. Teşekkürler @AliFarhoudi – Tom

cevap

0

Tekli ve çiftleri ayırmaya çalışıyorsunuz. Ve bunları ayrı ayrı yazdırmak istiyorsunuz. Hepsini algılamamışken bunları ayrı ayrı basamazsınız. Sonra tüm kategorileri tespit etmeli ve bundan sonra bunları ayrı ayrı yazdırabilirsiniz. Bunu yapmak için saptama üzerine kategorileri kaydetmeli ve algılandıktan sonra kaydedilmiş kategorileri yazdırmalısınız.

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; 
    } 
} 

Düzenleme 1.: değişkenlerinin 2'den fazla çeşit olsaydı , o zaman onları bir diziye tutabilir.

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

Teşekkürler! Ama ya tekler, çiftler, üç katmanlar ... sonsuz ise? – Tom

+0

Demek istediğim. Tekler ve çiftler veya daha fazlası olabilir. – Tom

+0

Teşekkürler! İşe yarıyor! – Tom