0

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

12
  • Do you want to make it manually or automatically? Commented Aug 23, 2017 at 14:12
  • automatically @MrProPop Commented Aug 23, 2017 at 14:13
  • You want to remove only one element from those which have same value, but which one? You should tell us that, but also you should try to do it yourself first instead of asking us to do it for you. There are tons of easy docs on the web about php arrays Commented Aug 23, 2017 at 14:13
  • php.net/manual/en/function.array-unique.php Commented Aug 23, 2017 at 14:14
  • I try before upload that please provide me that doc if you have that .. and in my question everything is clearly mention @Kaddath Commented Aug 23, 2017 at 14:14

5 Answers 5

1

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

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

5 Comments

I want to remove one value from the array how can I do that? For example In my array you see there are multiple 0 values. and I want to remove only one
I edited the answer this should remove only one duplicate
A foreach and two in_array make this a costly operation :)
@Martijn if he wants to remove one duplicate for each value, he will need a loop even with Don't Panic's solution. Not saying this answer is better, but you're not right if you think array_search is less costly than in_array. By the way, if he doesn't want to remove all duplicates, Don't Panic's solution would also need to track elements removed in some way or another
It still is constly. But that is because of the structure of his data, it's not really doable in an efficient way
1

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.

2 Comments

I agree with this approach in general, but I think the OP is asking about reducing the quantity of one item by one, not removing the item altogether.
Added that demo as well :)
0

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

2 Comments

Thank you its working. This is what I am looking for
You're welcome. The answer by @Martijn does contain some good advice, though, if this is for a sort of shopping cart app. Using the item id as the key may be easier to work with.
0
<?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;



?>

Comments

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

So first occurring value 0 will delete . Try this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.