Cześć,

Chciałem zrobić porównanie produktów z dwóch sklepów. Szukam produktów po kodzie produktów. Jeżeli jest podobny to porównuje liczbę produktów. Problem polega na tym że jak print poszczególne zmienne to wyniki sa raczej poprawne. Jednakże jak wyświetla zawartośc tablicy to wartości są dziwne. Na pierwszy rzut oka są powielone....

Cały kod:
  1. $conn1->close();
  2. $conn2->close();
  3.  
  4. $match = array();
  5. $new = array();
  6.  
  7. foreach ($result_source AS $source) {
  8.  
  9. $found = false;
  10. $code_source = substr($source['code'], 4);
  11. foreach ($result_target AS $target) {
  12. $code_target = substr($target['code'], 4);
  13. if ($code_source === $code_target) {
  14. if ($source['quantity'] !== $target['quantity']) {
  15. print $source['quantity'] .' -> ' . $target['quantity']."<br /><br />";
  16. $match[] = array('code' => $source['code'], 'quantity' => $source['quantity'], 'targetid' => $target['id'], 'sourceid' => $source['id']);
  17. }
  18. $found = true;
  19. break;
  20. }
  21. }
  22.  
  23. if (!$found) {
  24. $new[] = array('code' => $source['code'], 'quantity' => $source['quantity'], 'sourceid' => $source['id']);
  25. }
  26. }
  27. $msg = '';
  28. foreach ($match AS $entry) {
  29. $msg .= 'Change identified: Home_ID=' . $entry['sourceid'] . ' code: ' . $entry['code'] . ' quantity:' . $entry['quantity'] . PHP_EOL . '<br />';
  30. #print $msg;
  31. }
  32.  
  33. foreach ($new AS $entry) {
  34. $msg .= 'New Entry: Home_ID=' . $entry['sourceid'] . ' code: ' . $entry['code'] . ' quantity:' . $entry['quantity'] . PHP_EOL . '<br />';
  35. #print $msg;
  36. }


Wynik dla
  1. print $source['quantity'] .' -> ' . $target['quantity']."<br /><br />";


Znajduje się pod adresem: http://suszek.info/projekt1/

Jeżeli wyświetlę
  1. foreach ($match AS $entry) {
  2. $msg .= 'Change identified: Home_ID=' . $entry['sourceid'] . ' code: ' . $entry['code'] . ' quantity:' . $entry['quantity'] . PHP_EOL . '<br />';
  3. print $msg;
  4. }


Wynik znajduje się pod adresem: http://suszek.info/projekt1/index_1.php

Wydaje mi się że źle dodaje elementy do tablicy $match stąd jest aż tyle wpisów.

Z góry dzięki za pomoc.
Pozdrawiam

----edycja
Pomimo instrukcji warunkowych skrypt tworzy mi puste tablice: http://suszek.info/projekt1/index_1_1.php

  1. if ($code_source === $code_target) {
  2. if ($source['quantity'] != $target['quantity']) {
  3. $test[] = array('code_source' => $source['code'], 'quantity_source' => $source['quantity'], 'targetid' => $target['id'], 'sourceid' => $source['id'], 'targetcode' => $target['code']);
  4. array_push($match, $test);
  5.  
  6. }


Wydaje mi się że tutaj jest problem. Jest ktoś w stanie pomóc ?