Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i make my delete function work in php

my code was working but i don’t know how to create a delete function i tried my best to search some but it doesn’t fit in this code im still learning the basics and i will really appreciate the help, thank you very much in advance

<?php
    $straw = [];
    $index = 0;
    class Fruit {
      private $name;
      private $color;
    
      public function describe($name, $color) {
        $this->name = $name;
        $this->color = $color;
      }
    
      public function intro() {
         echo "{$this->name}"."\n";
        echo "{$this->color}"."\n";
      }
    }
    
    // Strawberry is inherited from Fruit
    class Strawberry extends Fruit {
      
      public function getfruit() {
        echo $this->intro();
       }
       public function assignfruit($name, $color){
      $this->describe($name, $color);
      }
      public function deletePatient($index){
            unset($this->intro[$index]);
        }
    }
    $strawberry1 = new Strawberry();
    $strawberry1->assignfruit("Strawberry", "red");
    $straw[$index]= $strawberry1;
    $index++;
    $strawberry2 = new Strawberry();
    $strawberry2->assignfruit("Strawberry", "red");
    $straw[$index]= $strawberry2;
    $index++;
    $strawberry2->deletePatient(1);
    
    foreach ($straw as $star){
      echo $star->getfruit();
      
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Seems like you want to delete a fruit?

You store the fruits in an array called $straw. So if you want to delete one, you need to delete it from that array.

Try this:

unset($straw[0]);
foreach ($straw as $star) {
    echo $star->getfruit();
      
}

That should delete the first entry into the array, and therefore your first fruit.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading