try

$array = json_decode( json_encode($array), true);

EDIT: I've tested this case, and it works:

$stdClass= new stdClass();
$stdClass->test = "foo";
$array = Array(
    "a" => Array("b","c"),
    "d" => $stdClass
);

$array = json_decode( json_encode($array), true);

var_dump($array);

OUTPUT

array
  'a' => 
    array
      0 => string 'b' (length=1)
      1 => string 'c' (length=1)
  'd' => 
    array
      'test' => string 'foo' (length=3)
Answer from Luca Rainone on Stack Overflow
Discussions

Remove empty stdclass object from array in PHP - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
January 25, 2019
php - how can i remove array from STDObject? - Stack Overflow
Hello!! i think its very simple question for those who familier with std objects and arrays in PHP. Here is my STD Object array: Array ( [0] => stdClass Object ( [name] => name1 [descripti... More on stackoverflow.com
🌐 stackoverflow.com
arrays - Remove string from an stdClass object in php - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most · Connect and share knowledge within a single location that is structured and easy to search More on stackoverflow.com
🌐 stackoverflow.com
August 31, 2016
php - Remove stdclass object from array second time always fails - Stack Overflow
Can you please help me find a problem in my code. I have a function for deleting images from my json string. I will provide my code with explanation. This is my test controller for testing my code... More on stackoverflow.com
🌐 stackoverflow.com
February 4, 2016
🌐
W3Docs
w3docs.com › php
PHP : Remove object from array | W3Docs
In this example, an array of three stdClass objects is created, with the first being the target object. The array_search function is used with strict comparison (true) to locate the exact instance of $objectToRemove. If found, unset removes it, and array_values reindexes the array to maintain sequential numeric keys. A message confirms the removal. If not found, a message indicates it wasn't found. Note on object comparison: In PHP, objects are compared by reference (identity) by default.
🌐
Laracasts
laracasts.com › discuss › channels › code-review › how-to-remove-stdobject-when-converting-collection-to-array
How to remove stdObject when converting collection to array?
Be part of JetBrains PHPverse 2026 ... bringing PHP devs worldwide together.Register Now ... array:2 [ 0 => {#2993 +"item_id": 11 +"item_code": "A1B2C3" } 1 => {#2994 +"item_id": 11343 +"item_code": "A1B2C4" } ] } But I can't access it normally for example in a for each loop: foreach ($data as $item) { echo ($item['item_id']) } I get Object of class stdClass could not ...
🌐
shareitHQ
shareithq.wordpress.com › tag › php-php-remove-an-element-from-a-stdclass-array-of-stdclass-objects
php PHP – Remove an element from a stdclass array of stdclass objects – shareitHQ
I want to remove an element (e.g the whole of [1]), but if I try to use unset, e.g unset($this->U16sArray[$arrayindex]) I get … · Fatal error: Cannot use object of type stdClass as array.
🌐
Stack Overflow
stackoverflow.com › questions › 39250962 › remove-string-from-an-stdclass-object-in-php
arrays - Remove string from an stdClass object in php - Stack Overflow
August 31, 2016 - I have an array called $field->selectoptions[1] print_r($field->selectoptions[1]) gives these results: stdClass Object ( [voption] => clabels [clabel] => Data Inizio [values] =>
Find elsewhere
🌐
YouTube
youtube.com › hey delphi
Array : Remove empty stdclass object from array in PHP - YouTube
Array : Remove empty stdclass object from array in PHPTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"Here's a secret feature...
Published: May 8, 2023
Views: 11
🌐
Stack Overflow
stackoverflow.com › questions › 31547355 › php-delete-element-from-objectstdclass
php delete element from object(stdClass) - Stack Overflow
July 22, 2015 - I am trying to delete some elements of an array resulting from a fetch from a function in a class: Copypublic function isChecked($db, $table, $id, $row_id) { $answer = $db->query("SELECT * FROM $table WHERE $row_id = $id")->fetch(); var_dump($answer); } ... Copyobject(stdClass)#2 (5) { ["act_prim"]=> string(2) "12" ["act_id"]=>string(1) "1" ["a"]=> NULL ["b"]=> string(1) "1" ["c"]=> string(1) "1" } I would like to remove the 2 first elements ("act_prim" and "act_id") from the array.
Top answer
1 of 12
64

You can do

function unsetValue(array $array, $value, $strict = TRUE)
{
    if(($key = array_search($value, $array, $strict)) !== FALSE) {
        unset($array[$key]);
    }
    return $array;
}

You can also use spl_object_hash to create a hash for the objects and use that as array key.

However, PHP also has a native Data Structure for Object collections with SplObjectStorage:

$a = new StdClass; $a->id = 1;
$b = new StdClass; $b->id = 2;
$c = new StdClass; $c->id = 3;

$storage = new SplObjectStorage;
$storage->attach($a);
$storage->attach($b);
$storage->attach($c);
echo $storage->count(); // 3

// trying to attach same object again
$storage->attach($c);
echo $storage->count(); // still 3

var_dump( $storage->contains($b) ); // TRUE
$storage->detach($b);
var_dump( $storage->contains($b) ); // FALSE

SplObjectStorage is Traversable, so you can foreach over it as well.

On a sidenote, PHP also has native interfaces for Subject and Observer.

2 of 12
22

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'));
🌐
Freelancer
freelancer.com › job search › how to remove stdclass object from array in php › 3
How to remove stdclass object from array in php Jobs, Employment | Freelancer
Ideal skills for this job include photo editing, attention to detail, and experience with object removal in images. Graphic Design Photo Editing Photography Photoshop Photoshop Design ... Hi, as discussed. I will be working on your 4 pages paper and remove plagiarism from it within 24-48 hours · Article Rewriting Powerpoint Report Writing Research Research Writing ... I need a PHP professional to improve the user interface of my existing website.
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › web development › php
Remove items from Array of objects on object keys - PHP
December 23, 2009 - Hello all, I have an array of objects that I need to remove some items and keep others. Here is my problem, I have this array:: array 0 => object(stdClass)[9] public 'id' => string '781' (length=3) public 'root' => string '781' (length=3) public 'RSS_Role' => string...
🌐
Debugcn
debugcn.com › en › article › 33062637.html
Remove empty stdclass object from array in PHP - DebugCN
$activities = json_decode('[{"id": 2088996538},{},{"id": 2077495673}]'); unset($activities[1]); $activities = array_values($activities); echo json_encode($activities); this actually works, how could I've missed it. Gonna try it out with the larger data set. Thanks!