Remove key from object array php
I agree with the answers above, but for the sake of completeness (where you may not have unique IDs to use as a key) my preferred methods of removing values from an array are as follows: Show
Which are used in the following way:
Hope that helps. View Discussion Improve Article Save Article View Discussion Improve Article Save Article In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically. Function Used:
Example 1:
Output: array(2) { [0]=> string(5) "geeks" [2]=> string(5) "geeks" } array(2) { [0]=> string(5) "geeks" [2]=> string(5) "geeks" } We can also
use array_splice() function which removes a portion of the array and replaces it with something else.
Output: array(2) { [0]=> string(5) "geeks" [1]=> string(5) "geeks" } PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. The unset function can be used to remove array object from a specific index in PHP − ExampleLive Demo $index = 2; $objectarray = array( 0 => array('label' => 'abc', 'value' => 'n23'), 1 => array('label' => 'def', 'value' => '2n13'), 2 => array('label' => 'abcdef', 'value' => 'n214'), 3 => array('label' => 'defabc', 'value' => '03n2') ); var_dump($objectarray); foreach ($objectarray as $key => $object) { if ($key == $index) { unset($objectarray[$index]); } } var_dump($objectarray); OutputThis will produce the following output − array(4) { [0]=> array(2) { ["label"]=> string(3) "abc" ["value"]=> string(3) "n23" } [1]=> array(2) { ["label"]=> string(3) "def" ["value"]=> string(4) "2n13" } [2]=> array(2) { ["label"]=> string(6) "abcdef" ["value"]=> string(5) "n214" } [3]=> array(2) { ["label"]=> string(6) "defabc" ["value"]=> string(5) "03n2" } } array(3) { [0]=> array(2) { ["label"]=> string(3) "abc" ["value"]=> string(3) "n23" } [1]=> array(2) { ["label"]=> string(3) "def" ["value"]=> string(4) "2n13" } [3]=> array(2) { ["label"]=> string(6) "defabc" ["value"]=> string(5) "03n2" } } An array with 4 objects is declared and assigned to variable ‘objectarray’. Here, we wish to remove the object from index 2, that is also declared with variable named ‘index’. The foreach loop is used to traverse through the array and when the index value in the traversal matches the index from where the value needs to be removed, the ‘unset’ function is called on that element and the remaining elements are returned as output.
Updated on 09-Apr-2020 11:10:50
How to remove key value in PHP?To remove a key and its respective value from an associative array in PHP you can use the unset() function. As the name of the function suggests, you use the unset() function to unset a given variable or in this case an array key with its value.
How to remove a particular element from an array in PHP?In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically. Function Used: unset(): This function unsets a given variable.
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.
What does Array_splice () function do give an example?The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Tip: If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter (See Example 2).
|