Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Porównanie dwóch tablic
rafal75
post
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 8.05.2021

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


Prosił bym o pomoc.
Posiadam dwie tablice. Sprzedaż i zakup. Muszę sprawdzić dla każdego elementu "SKU" w tablicy zakupów, czy nastąpiła sprzedaż. Jeśli tak, to efektem tego powinna być nowa tablica $tmp, ze zmodyfikowanym stanem "count".
Z góry dziękuję za pomoc.

$boughtProducts = array(
array("SKU"=>"0001", "count"=>10, "Joe"=>"43"),
array("SKU"=>"0002", "count"=>8, "Joe"=>"43"),
array("SKU"=>"0003", "count"=>10, "Joe"=>"43")
);
$soldProducts = array(
array("SKU"=>"0001", "count"=>10, "Joe"=>"43"),
array("SKU"=>"0002", "count"=>2, "Joe"=>"43"),
array("SKU"=>"0003", "count"=>0, "Joe"=>"43")
);
$tmp= array(
array("SKU"=>"0001", "count"=>10, "Joe"=>"43"),
array("SKU"=>"0002", "count"=>6, "Joe"=>"43"),
array("SKU"=>"0003", "count"=>0, "Joe"=>"43")
);

Dla zainteresowanych. Rozwiązałem problem.

$boughtProducts = array(
array("SKU"=>"0001", "count"=>10, "Joe"=>"43"),
array("SKU"=>"0002", "count"=>8, "Joe"=>"43"),
array("SKU"=>"0003", "count"=>10, "Joe"=>"43")
);
$soldProducts = array(
array("SKU"=>"0003", "count"=>2, "Joe"=>"43"),
// array("SKU"=>"0002", "count"=>2, "Joe"=>"43"),
array("SKU"=>"0001", "count"=>5, "Joe"=>"43")
);


$items = count($boughtProducts);
$itemsb = count($soldProducts);
$result = $boughtProducts;
for($i = 0; $i < $items; $i++) {
$b=0;
if($result[$i]["SKU"] != $soldProducts[$b]['SKU']) {
for($b = 0; $b < $itemsb; $b++){
if ($soldProducts[$b]['SKU'] == $result[$i]["SKU"])
{
$result[$i]["count"] = ($boughtProducts[$i]['count'] - $soldProducts[$b]['count']);
}
}
}else{
$result[$i]["count"] = ($boughtProducts[$i]['count'] - $soldProducts[$b]['count']);
}
}
echo "<pre>";
print_r($result);
echo "</pre>";

Ten post edytował rafal75 12.05.2021, 20:07:58
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: 5.10.2025 - 07:38