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
 
Start new topic
Odpowiedzi
gitbejbe
post
Post #2





Grupa: Zarejestrowani
Postów: 516
Pomógł: 63
Dołączył: 27.08.2012

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


czasami nie ma co tracić czasu na wymyślanie czegoś, co już istnieje i jest łatwo dostępne. Google ma pełno podpowiedzi i gotowców. Na twój problem polecam frazę "php get all combinations of array".

wycięte z neta:
  1. print_r(AllPermutations(array('peter', 'paul', 'mary')));
  2.  
  3. function AllPermutations($InArray, $InProcessedArray = array())
  4. {
  5. $ReturnArray = array();
  6. foreach($InArray as $Key=>$value)
  7. {
  8. $CopyArray = $InProcessedArray;
  9. $CopyArray[$Key] = $value;
  10. $TempArray = array_diff_key($InArray, $CopyArray);
  11. if (count($TempArray) == 0)
  12. {
  13. $ReturnArray[] = $CopyArray;
  14. }
  15. else
  16. {
  17. $ReturnArray = array_merge($ReturnArray, AllPermutations($TempArray, $CopyArray));
  18. }
  19. }
  20. return $ReturnArray;
  21.  
  22. //LUB
  23.  
  24. function pc_permute($items, $perms = array()) {
  25. if (empty($items)) {
  26. echo join(' ', $perms) . "</br>";
  27. } else {
  28. for ($i = count($items) - 1; $i >= 0; --$i) {
  29. $newitems = $items;
  30. $newperms = $perms;
  31. list($foo) = array_splice($newitems, $i, 1);
  32. array_unshift($newperms, $foo);
  33. pc_permute($newitems, $newperms);
  34. }
  35. }
  36. }
  37.  
  38. $arr = array('peter', 'paul', 'mary');
  39.  
  40. pc_permute($arr);
  41.  
  42. }


Ten post edytował gitbejbe 10.02.2014, 22:05:39
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 7.10.2025 - 17:10