How can I remove a single array member using array_splice in php?
Asked Answered
C

5

9

I think I may not understand correctly how array_splice is supposed to work. My understanding is that the first param is your initial array, the second param is the element to start at, and the third param is the length, or number of elements to remove/replace.

So, I have this array (print_r output):

Array ( 
[0] => Array ( [TypeFlag] => S [qty] => 1 [denom] => 25 [certMessage] => [totalPrice] => 25 ) 
[1] => Array ( [TypeFlag] => C [qty] => 2 [denom] => 25 [certMessage] => [totalPrice] => 50 ) 
[2] => Array ( [TypeFlag] => V [qty] => 2 [denom] => 25 [certMessage] => test [totalPrice] => 50 ) )

I want to completely remove the second element (the array with index of 1; TypeFlag = C, etc.) I do not want to replace it with anything; just to return the array with the remaining two elements. I've tried this (where cart is the array name):

$cart = array_splice($cart, 1,1);

But what I end up with is this when doing a print_r:

Array ( [0] => Array ( [TypeFlag] => C [qty] => 2 [denom] => 25 [certMessage] => [totalPrice] => 50 ) ) 

So it appears to be removing 0 and 2, and leaving 1 as the remainder. What am I doing wrong?

Chitter answered 27/9, 2010 at 15:8 Comment(0)
R
16

array_splice returns an array consisting of the extracted elements.

You are doing:

$cart = array_splice($cart, 1,1);

So you are extracting 2nd element (index 1) and are assigning the result back to $cart, overwriting your original array.

To completely remove the 2nd element do not assign the return value of array_splice back to $cart. So just do:

array_splice($cart, 1,1);

This works because the array is passed by reference to the function.

Also if you want to remove a single element from the array its more efficient and cleaner to use unset as:

unset($cart[1]);
Refurbish answered 27/9, 2010 at 15:15 Comment(3)
So if I'm doing this inside a function and need to return the array minus the chosen value, do I need to first assign $cart to a temporary value (i.e. $cartTemp = $cart), then do the splice, then reassign the array to $cart? Or do I just do array_splice($cart, 1, 1), then return $cart? NEVER MIND - just tried it and it works if I just do array_splice and then return $cart. Thanks.Chitter
No. There is no need to assign it to a temp. Just call the function, passing it the array and the function will change the array.Refurbish
unset needs indexed arrays, if the array has no indexes unset adds them by itself and if you want your collection without indexes it messes everything up..Zoes
S
5

array_splice returns the "removed/replaced" elements, which you're then assigning to overwrite the correctly spliced $cart

$discard = array_splice($cart, 1,1);

or simply

array_splice($cart, 1,1);
Saveall answered 27/9, 2010 at 15:14 Comment(0)
C
3

Why dont you use unset- i.e.

unset(nameofyourarray[indextoremove]);

http://php.net/manual/en/function.unset.php

Conflict answered 27/9, 2010 at 15:27 Comment(0)
A
3

The only issue with using unset() is that it doesn't update the indexes.

i.e.

$arr = array('one', 'two', 'three', 'four');
print_r($arr);

Array ( [0] => one [1] => two [2] => three [3] => four )

unset($arr[2]);

print_r($arr);

Array ( [0] => one [1] => two [3] => four )

whereas array_splice will re-index the array

$arr = array('one', 'two', 'three', 'four');
array_splice($arr,2,1);
print_r($arr);

Array ( [0] => one [1] => two [2] => four )

Adrenocorticotropic answered 14/1, 2015 at 2:40 Comment(0)
S
2

Just use unset():

unset($cart[1]);
Shandrashandrydan answered 27/9, 2010 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.