Mark J
Mark J

Reputation: 31

Search array, and if found

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
        )

)

Hello guys,

I need some help with this. I am creating a shop system. And i am trying to add products to a basket/cart.

I need to check if the product number is in the array, if yes? then is the size the right one? - if yes, then update the right one..

if the size is not the right add a new.

If the product number isn't in the array .. add a new

I have tryed with in_array, foreach, and some stuff.. i cant get it to work. Can any of you help me ?

it is in php

Thanks a lot ;)

Upvotes: 0

Views: 52

Answers (2)

Alex Perrin
Alex Perrin

Reputation: 370

Here is my implementation:

$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);

Upvotes: 1

The Code Dad
The Code Dad

Reputation: 19

I'm not sure if you have the ability to do this, but if you do you could try the following. Key your array by your product number and use the array_key_exists() function to determine if that key is present.

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
    //
}

Reference: http://php.net/manual/en/function.array-key-exists.php

Upvotes: 0

Related Questions