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:
/**
* Remove each instance of a value within an array
* @param array $array
* @param mixed $value
* @return array
*/
function array_remove[&$array, $value]
{
return array_filter[$array, function[$a] use[$value] {
return $a !== $value;
}];
}
/**
* Remove each instance of an object within an array [matched on a given property, $prop]
* @param array $array
* @param mixed $value
* @param string $prop
* @return array
*/
function array_remove_object[&$array, $value, $prop]
{
return array_filter[$array, function[$a] use[$value, $prop] {
return $a->$prop !== $value;
}];
}
Which are used in the following way:
$values = array[
1, 2, 5, 3, 5, 6, 7, 1, 2, 4, 5, 6, 6, 8, 8,
];
print_r[array_remove[$values, 6]];
class Obj {
public $id;
public function __construct[$id] {
$this->id = $id;
}
}
$objects = array[
new Obj[1], new Obj[2], new Obj[4], new Obj[3], new Obj[6], new Obj[4], new Obj[3], new Obj[1], new Obj[5],
];
print_r[array_remove_object[$objects, 1, 'id']];
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:
- unset[]: This function unsets a given variable.
Syntax:void unset [ mixed $var [, mixed $... ] ]
- array_values[]: This function returns all the values from the array and indexes the array
numerically.
Syntax:array array_values [ array $array ]
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.
Example 2:
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 −
Example
Live 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];
Output
This 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
- Related Questions & Answers
- Remove object from array in MongoDB?
- How to remove object from array in MongoDB?
- Search by id and remove object from JSON array in JavaScript
- PHP print keys from an object?
- Remove number properties from an object JavaScript
- Remove new lines from string in PHP
- Remove extra whitespace from the beginning PHP?
- Remove comma from a string in PHP?
- JavaScript Remove random item from array and then remove it from array until array is empty
- Convert object to an array in PHP.
- Remove a character from a Java StringBuffer object
- Remove null element from MongoDB array?
- Remove duplicated elements of associative array in PHP
- Convert an object to associative array in PHP
- Create array from JSON object JavaScript