I am trying to figure out how to remove one main element and all its siblings and save the array afterwards.
Here is what i got:
$my_array = Array
(
[0] => Array
(
[username] => Pete
[userid] => 2
)
[1] => Array
(
[username] => James
[userid] => 4
)
[2] => Array
(
[username] => John
[userid] => 3
)
)
Now, what i want to do is to remove the element in where I have the userid 4 and then save it all back into $my_array like this:
$my_array = Array
(
[0] => Array
(
[username] => Pete
[userid] => 2
)
[2] => Array
(
[username] => John
[userid] => 3
)
)
Can this be done? and if yes... How???
Thanks in advance :-)
pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.
To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.
For removing one array from another array in java we will use the removeAll() method. This will remove all the elements of the array1 from array2 if we call removeAll() function from array2 and array1 as a parameter.
To delete a specific element from an array, a user must define the position from which the array's element should be removed. The deletion of the element does not affect the size of an array.
Try this:
foreach ($array as $key => $value) {
if ($value["userid"] == 4) { unset($array[$key]); }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With