Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Jak zrobić rekurencyjny foreach, rezultat w ostatniej pętli
radiopraca
post
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 1
Dołączył: 7.02.2014

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


Jak napisać funkcję która robi foreach k-razy.
Przesyłam przykład z k = 4

  1. foreach ($aaa as $aa) {
  2. $newProducts = array_diff($products, $aa);
  3. $newProducts = array_values($newProducts);
  4. $bbb = go2($newProducts);
  5.  
  6. foreach ($bbb as $bb) {
  7. $newProducts2 = array_diff($newProducts, $bb);
  8. $newProducts2 = array_values($newProducts2);
  9. $ccc = go2($newProducts2);
  10.  
  11. foreach ($ccc as $cc) {
  12. $newProducts3 = array_diff($newProducts2, $cc);
  13. $newProducts3 = array_values($newProducts3);
  14.  
  15. $out[] = array(
  16. $aa,
  17. $bb,
  18. $cc,
  19. $newProducts3
  20. );
  21. }
  22. }
  23. }


Rekurencyjne FOR-y już kumam i sobie radze, ale tutaj cała akcja toczy się w ostatnim foreachu.
Walcze z tym już od rana, na razie mam tak, ale to nie działa, pomoze ktoś?


  1. function xxx($products, $partials, $i = 0, $part) {
  2.  
  3. if ($i < 1) {
  4. foreach ($partials as $part) {
  5. $newProducts = array_diff($products, $part);
  6. $newProducts = array_values($newProducts);
  7. $bbb = go2($newProducts);
  8.  
  9. $partial = array_megre($part, $bbb);
  10.  
  11. xxx($newProducts, $bbb, ++$i, $partial);
  12. }
  13. }
  14. }
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: 21.08.2025 - 23:22