There's 2 approaches I've found to recreate this, and only one has worked when using unset.
Approach 1 - Using (object)[];
$test = [
(object)[
"1" => "1",
"2" => "2"
];
Approach 2 - Using stdClass():
$array = new stdClass();
$array->{"1"} = "1";
$array->{"2"} = "2";
$test = [
$array
];
At this point, I try to unset() the 2nd key of the first entry in $test:
unset($test[0]->{"2"});
dd($test);
The output of dd($test); following this unset() is:
// Approach 1
array:1 [▼
0 => {#407 ▼
+"1": "1"
+"2": "2"
}
]
// Approach 2
array:1 [▼
0 => {#407 ▼
+"2": "2"
}
]
It seems that using unset() works as expected when using stdClass(), but if you're casting an array as an object like I tried in Approach 1, unset() will look to work but not actually do anything.
The only way I could get Approach 1 to work is like so:
foreach($test AS $tKey => $array){
$array = (array)$array;
foreach($array AS $aKey => $value){
if($key == 2){
unset($array[$aKey]);
}
}
$test[$tKey] = (object)$array;
}
Which works, but seems inefficient as I'm casing the casted array to an array then back to an object. Hopefully this gives you some insight into the issue here.
array_spliceandunsetdd($test)isdie(var_dump($test)); laravel function. See first section of the question above.unset$test = [(object)["1" => "1", "2" => "2"]];, and then usingunset($test[0]->{"1"});doesn't produce an error, but also doesn't unset the property.