Azad Chouhan
Azad Chouhan

Reputation: 290

How to remove only one element from array in multiple duplicates in php

I am writing code for add and remove array in php. The arrays are pushing succesfully according to my requirement. Now I want to remove the array element. But Here is the condition. I have duplicate values in this array. So I just want to remove only one element of duplicate values not all

Here is the code

<?php
session_start();
$id = $_POST['value'];

$_SESSION['id'] = $id ;

if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
array_push($_SESSION['cart'], $id);


echo true;



?>

Here is the array I am getting

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 0 [4] => 1 [5] => 1 [6] => 6 [7] => 0 [8] => 0 [9] => 2 [10] => 0 [11] => 0 [12] => 5 [13] => 1 [14] => 1 ) 

For example. I have multiple elements of 0 in this array. So I want to remove only one element not all.

Thanks

Upvotes: 0

Views: 363

Answers (5)

Martijn
Martijn

Reputation: 16103

You should not continue as you are, but take time to reformat. You are a cart, this can be done easier and more future-proof. When this project increases in size and complexity, this will come back to haunt you (building 5 [small] shops taught me this).

Instead of adding a value which is the ID of a product, add a key which is the ID of the product, with an array of information about it, like quantity.

$_SESSION['cart'] = array();

// to add a product:
$_SESSION['cart'][ $product_id ] = array('quantity'=>5);

// To remove from your array:
unset($_SESSION['cart'][ $product_id ]);

// To change the amount
$_SESSION['cart'][ $product_id ]['quantity'] = $newValue;
// Or, alternatively:
$_SESSION['cart'][ $product_id ]['quantity'] += 1; // add one.

Upvotes: 1

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

To remove one instance of an ID from $_SESSION['cart'], you can use array_search to look for the posted value in that array and return its key if it's found.

$key = array_search($_POST['value'], $_SESSION['cart']);

array_search will return the first matching key, (so the key for just one item).

If a key is found, unset that key, and you'll remove only one of the corresponding value.

if ($key !== false) unset($_SESSION['cart'][$key]);

Upvotes: 0

Minas Jean
Minas Jean

Reputation: 107

if (($key = array_search(0, $array)) !== false) {
    unset($array[$key]);
}

So first occurring value 0 will delete . Try this

Upvotes: 0

Momo Kebab
Momo Kebab

Reputation: 212

Try this :

$newarray = array();
$alreadydeleted = array();
foreach ($oldarray AS $key => $value)
{
    if (!in_array($value, $newarray) || in_array($value, $alreadydeleted))
        $newarray[$key] = $value;
    else
        $alreadydeleted[$key] = $value;
}

And you should have your new array with one duplicate removed

Upvotes: 1

Keyur Padalia
Keyur Padalia

Reputation: 2097

<?php
session_start();
$id = $_POST['value'];

$_SESSION['id'] = $id ;

if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}

//You can try this
if(!array_search($id,$_SESSION['cart'],TRUE))
{
    array_push($_SESSION['cart'], $id);
}

//OR can do this after array_push
$new_unique_array = array_unique($_SESSION['cart']);


echo true;



?>

Upvotes: 0

Related Questions