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:

/**
 * 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

  • Read
  • Discuss
  • 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:

    1. unset(): This function unsets a given variable.
      Syntax:
      void unset ( mixed $var [, mixed $... ] )
    2. array_values(): This function returns all the values from the array and indexes the array numerically.
      Syntax:
      array array_values ( array $array )

    Example 1:

    $arr1 = array(

        'geeks',

        'for',

        'geeks'

    );

    unset($arr1[1]); 

    var_dump($arr1);

    $arr2 = array_values($arr1);

    var_dump($arr2);

    ?>

    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:

    $arr1 = array(

        'geeks',

        'for',

        'geeks'

    );

    array_splice($arr1, 1, 1); 

    var_dump($arr1);

    ?>

    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.

    Remove key from object array php

    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

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