$followers = [['id'=>0], ['id'=>1]];
So, assuming I have this array, what would be the best way to remove the object by it's id?
That's what i came up with
foreach($followers as $key=>$follower){
if($follower->id == 0){unset $followers[$key]}
}
is there a better more efficient way?
It is an array not an object, so why are you accessing as an object?
Try this,
foreach ($followers as $key => $follower) {
if($followers[$key] == 0) {
unset($followers[$key]);
}
}
You were accessing values of an array like an object.
You were using unset incorrectly.
You were missing the ; at the end of your unset part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With