I have a storefront website, and I want to keep track of which products a person has purchased in their user table. When a person buys something, appended to a column will be the product ids of the stuff they've purchased. I want to change this data using explode(), but I'm not sure how to increase the size of an array after it has been initialized.
//this is when a user purchases a new product $newItems = #whatever they're purchasing //grab previous data from SQL ("1,2,3") $purchProd = explode(',',$dataFromSQL); //$purchProd now an array of size 3 for ($i = 0; $i < count($newItems); $i++) { $purchProd[$i] = $newItems[$i]; } There's two problems here:
- I can't "resize" an array like this, it will throw an out of bounds exception
- I don't want
$purchProdto have duplicates; if user buys another item1, this is no good
I could use in_array to filter out duplicates and only add new entries, but then I come across an instance where I'll have blanks.
//$purchProd = {1,2,3}; //$newItems = {4,1,5}; for ($i = 0; $i < count($newItems); $i++) { if (!in_array($newItems[$i],$purchProd) { $purchProd[$i] = $newItems[$i]; } } //$purchProd = {1,2,3,4,null,5}; But that's only if I could resize an array dynamically, which I can't anyways.
I'm stumped. Is there a method to get my desired result? Basically I want to add all elements in array2 but not in array1 into array1 if that makes sense.
Clarification
Basically I have two arrays:
$arrayOne = {1,2,3}; $arrayTwo = {4,1,5}; I want to make $arrayThree which contains all elements from $arrayOne and all elements from $arrayTwo, but no duplicates. A potential $arrayThree would be: {1,2,3,4,5}, notice the second 1 was not added.
productsPurchaseand one elementproductsReviewed. When a person goes to review a new product, I would grab both from SQL, explode them, and compare them, ensuring an end-user cannot review the same item twice. This is why it is stored in text, so I can explode it and do the comparison.(user_id, product_id)prevents dups in theproduct_reviewtable.