2017-03-15 11 views
0
Array 
(
    [0] => Array 
     (
      [product_number] => 1000000002 
      [quantity] => 1 
      [size] => L 
     ) 

    [1] => Array 
     (
      [product_number] => 1000000002 
      [quantity] => 1 
      [size] => XS 
     ) 

    [2] => Array 
     (
      [product_number] => 1000000002 
      [quantity] => 1 
      [size] => M 
     ) 

) 

こんにちはたみんな、検索配列、および

私はこれでいくつかの助けが必要な場合。私は店舗システムを作っています。 そして、バスケット/カートに商品を追加しようとしています。

はいの場合、製品番号が配列に含まれているかどうか確認する必要がありますか?サイズは正しいですか? - そうなら、正しいものを更新してください。

サイズが正しくない場合は、新しいものを追加してください。

製品番号が配列になっていない場合..私はin_array、foreachの、およびいくつかのものにしようと試みてきた新しい

を追加..私はそれが仕事を得る傾けます。あなたの誰かが私を助けることができますか?

それはPHPで

おかげでたくさんです。)

+2

は、私は、問題はそれを実行する必要がありsecound検索と思われるもの言語やあなたの未遂コード –

答えて

1

は私の実装です:

$ShoppingBasket = Array 
(
    Array 
     (
      "product_number" => 1000000002, 
      "quantity" => 1, 
      "size" => "L" 
     ), 
    Array 
     (
      "product_number" => 1000000002, 
      "quantity" => 1, 
      "size" => "XS" 
     ), 
    Array 
     (
      "product_number" => 1000000002, 
      "quantity" => 1, 
      "size" => "M" 
     ) 

); 

function addItem(&$ShoppingBasket, $PN, $size) 
{ //use references with & to modify the array directly 
    // loop through each element of your array (mind this is an array of hashes. 
    foreach ($ShoppingBasket as &$item) //reference again so it modifies the item direclty in $ShoppingBasket. 
    { 
     // compare if the PN and size are the same 
     if ($item["product_number"] == $PN && strcmp($item["size"],$size) == 0) 
     { 
       $item["quantity"] ++; //increase the quantity count 
       return; //end the function if we found the item. 
     } 
    } 
    // this acts as a else close, it only executes if we haven't found anything already. 
    // push a new hash in $ShoppingBasket where all the field are specified. 
    array_push($ShoppingBasket, array("product_number" => $PN,"quantity" => 1,"size" => $size)); 
    return; 
} 
function removeItem(&$ShoppingBasket, $PN, $size) 
{ //use references with & to modify the array directly 
    for($i = 0 ; $i < count($ShoppingBasket) ; $i++) 
    {//need to interate with a counter to have the position in the array 
     if ($ShoppingBasket[$i]["product_number"] == $PN && strcmp($ShoppingBasket[$i]["size"],$size) == 0) 
     { 
       array_splice($ShoppingBasket, $i, 1); 
       //splice removes 1 element from position $i 
       return TRUE; //we have removed something 
     } 
    } 
    return FALSE; // we have not removed the item 
} 

//little test of the function 
addItem($ShoppingBasket,1000000002, "M"); 
addItem($ShoppingBasket,1000000002, "M"); 
addItem($ShoppingBasket,1000000002, "XXL"); 
addItem($ShoppingBasket,1000000232, "M"); 
print_r($ShoppingBasket); 
removeItem($ShoppingBasket,1000000232, "M"); 
removeItem($ShoppingBasket,1000000002, "M"); 
removeItem($ShoppingBasket,1000000002, "XXL"); 
print_r($ShoppingBasket); 
+0

私はあなたに感謝の気持ちがわからないのですが、あなたはあなたの製品に簡単にアクセスできるので、 私は6時間使用しました。私は..これを曇らせようとしています。 私のコードを説明してもらえますか? - 新しいものを学ぶ? –

+0

私は今ほとんど泣いているよ! :D –

+0

私はコードにコメントしました。あなたが理解するのにまだ苦労しているのであれば、私はあなたがPHPにとって少し新しいと思います。あなたが練習でそれを得ることを心配しないでください。 –

0

私はあなたがこれを行うための能力を持っているかどうかわからないんだけど、あなたが行う場合は、以下のことを試みることができます。製品番号で配列をキー入力し、array_key_exists()関数を使用してそのキーが存在するかどうかを判断します。

if(array_key_exists($product_number, $product_array)) 
{ 
    // 
    // The product number was found in the array of products 
    // 
} 

else 
{ 
    // 
    // The product number was not found in the array of products 
    // 
} 

参考:ここでhttp://php.net/manual/en/function.array-key-exists.php

+0

を投稿教えてください。.. –

+0

($ product_array [$ product_number] ['size'] == '){} –

関連する問題