-2

I'm getting this error Error: Call to a member function remove() on array in file

Getting this Array

How can I remove specific data from array where clinic_id matches?

    $factory = (new Factory())->withServiceAccount($app)
        ->withDatabaseUri('db');
    $database = $factory->createDatabase();
    $reference = $database->getReference('list_notification')->getValue();

   foreach ($reference as $ref){
        if($ref["clinic_id"] === $clinicId){
            $ref->remove();
        }
    }
Daniyal Mughees
  • 303
  • 7
  • 21
  • you're calling a methos on an array !! what is the answer you're expecting to get ? you can check the variables nature yourself using some `dd($variable)` – N69S Jul 19 '21 at 07:52
  • Yes, But I'm getting array what I can do to remove specific array? – Daniyal Mughees Jul 19 '21 at 08:12
  • Does this answer your question? [Deleting an element from an array in PHP](https://stackoverflow.com/questions/369602/deleting-an-element-from-an-array-in-php) – N69S Jul 19 '21 at 08:15

2 Answers2

1
    $factory = (new Factory())->withServiceAccount($app)
        ->withDatabaseUri('https://dental-notification-efd83-default-rtdb.firebaseio.com/');
    $database = $factory->createDatabase();
    $references = $database->getReference("list_notification")->getValue();

    foreach ($references as $key){
        if($key["clinic_id"] == $clinicId){
            $database->getReference('list_notification/'. $key["unique_id"])->remove();
        }
    }
Daniyal Mughees
  • 303
  • 7
  • 21
0

To remove an element from an array as any variable, use unset()

$reference = $database->getReference('list_notification')->getValue();

foreach ($reference as $key => $ref){
    if($ref["clinic_id"] === $clinicId){
        $database->getReference('list_notification/'.$key)->delete();
    }
}
N69S
  • 16,110
  • 3
  • 22
  • 36