Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] wszystkie kombinacje elementów tablicy bez powtórzeń z dowolną liczbą elementów, pomoże ktoś kod uprościć?
adi456
post
Post #1





Grupa: Zarejestrowani
Postów: 251
Pomógł: 0
Dołączył: 21.01.2010
Skąd: Nowy Sącz

Ostrzeżenie: (0%)
-----


doszedłem do 4 elementów, jeżeli mam dopisywać ich więcej to będzie chore, podejrzewam że można to jakoś uprościć, kod działa w 100% prawidłowo

  1. $model='1 2 3 4';
  2.  
  3. $model_tabl = str_replace('-', ' ', $model);
  4. $model_tabl = explode(" ", $model_tabl);
  5.  
  6. $ile=count($model_tabl);
  7.  
  8. for( $x = 0; $x < $ile; $x++ ) {
  9. for( $x1 = 0; $x1 < $ile; $x1++ ) {
  10. if(empty($model_tabl[2])) {
  11. if ($model_tabl[$x]!=$model_tabl[$x1]){
  12. echo $model_tabl[$x].' '.$model_tabl[$x1].'<br>';
  13. };
  14. }
  15. else {
  16. for( $x2 = 0; $x2 < $ile; $x2++ ) {
  17. if(empty($model_tabl[3])) {
  18. if ($model_tabl[$x]!=$model_tabl[$x1] && $model_tabl[$x1]!=$model_tabl[$x2] && $model_tabl[$x]!=$model_tabl[$x2]){
  19. echo $model_tabl[$x].' '.$model_tabl[$x1].' '.$model_tabl[$x2].'<br>';
  20. };
  21. } else {
  22. for( $x3 = 0; $x3 < $ile; $x3++ ) {
  23. if(empty($model_tabl[4])) {
  24. if ($model_tabl[$x]!=$model_tabl[$x1] && $model_tabl[$x1]!=$model_tabl[$x2] && $model_tabl[$x]!=$model_tabl[$x2] && $model_tabl[$x]!=$model_tabl[$x3] && $model_tabl[$x1]!=$model_tabl[$x3] && $model_tabl[$x2]!=$model_tabl[$x3]){
  25. echo $model_tabl[$x].' '.$model_tabl[$x1].' '.$model_tabl[$x2].' '.$model_tabl[$x3].'<br>';
  26. };
  27. } else {
  28. }
  29. }
  30. }
  31. }
  32. }
  33.  
  34. }
  35. }
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 14:21