1

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:

  1. I can't "resize" an array like this, it will throw an out of bounds exception
  2. I don't want $purchProd to have duplicates; if user buys another item 1, 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.

4
  • 2
    why not use while loops, and array_push and array_pop? Less is more. There is lots I don't understand about your dilemma. An example of data would be nice to help us understand exactly what goal you are trying to achieve. Another idea. If you sort your two array elements that goes a long way towards a functional comparison routine. Commented Apr 5, 2014 at 7:52
  • 1
    Storing row data in a serialised form usually ends up with problems, as sooner or later you want to perform some SQL on it, and find that you cannot. Why not store the purchase data as individual rows in the database? Commented Apr 5, 2014 at 8:07
  • @halfer, for simplicity/ease of use. I plan to have one element productsPurchase and one element productsReviewed. 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. Commented Apr 5, 2014 at 8:09
  • 2
    I'd say doing it my way is easier - this is exactly what databases are designed to do. A unique constraint on (user_id, product_id) prevents dups in the product_review table. Commented Apr 5, 2014 at 8:11

1 Answer 1

1

To answer your question, if you have two strings:

array_unique(array_merge(explode(',', $string1), explode(',', $string2))) 

This will join them together and throw away any duplicate values.

However, as per the comments, storing data in a serialised form in a relational database is usually the wrong answer1, as it locks your data into a form that has no type, that cannot have database constraints applied to it, and that cannot be searched using traditional SQL.

Instead I'd add a constraint onto the product_review table to prevent duplicates, and then do a SELECT check to prevent the user either seeing the review UI or adding a review.


1 This technique is good to squeeze out extra read performance, and should be used only when other approaches have been exhausted (e.g. using indexes and rewriting queries). Most people should not do it this way.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers. You've been a great help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.