Jump to content


This topic is now archived and is closed to further replies.


Delete specific substring

Recommended Posts

I'm trying to build an e-commerce type website...

I couldn't get a "shopping cart" array to store in a session variable, so I gave up and started on cookies instead. So.. I have a string that I'm getting from a cookie. It's in the format "1,2,1,3,4,5" and so on (no particular order) depending on what's been put into it.

I'm just wondering if theres any way to delete, for example, 1 instance of "2," or "5"? I can do it to remove all entries, but I only want it to do it once.

Share this post

Link to post
Share on other sites
Thought I should post the bit of code I'm dealing with.

$item = $_POST['id'];
$qty = $_POST['qty'];

if (strlen($cart)==1) {

echo "<meta content='0;url=index.php?p=checkout' http-equiv='refresh' />";

} else {

$cart = explode("|", $_COOKIE['cart']);
$cart_count = array_count_values($cart);

foreach ($cart_count as $itemNum => $qty) {

echo "<meta content='0;url=index.php?p=checkout' http-equiv='refresh' />";
Basically what I'm doing is using the array_count_values method to "group by" the item number.
So what I want to do is reduce the quantity for the item corresponding to the button that's been pressed and then regenerate the string and add it to the cookie again.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.