Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove element in multidimensional array and save [closed]

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 :-)

like image 545
Mansa Avatar asked Mar 21 '13 22:03

Mansa


People also ask

How do I remove a specific element from an array?

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.

How do you store elements in a 2-D 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.

How do you remove an element from an array from another array?

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.

Can we delete an element from an array?

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.


1 Answers

Try this:

foreach ($array as $key => $value) { 

    if ($value["userid"] == 4) { unset($array[$key]); }

}
like image 164
Madara's Ghost Avatar answered Sep 20 '22 04:09

Madara's Ghost