Skip to content

PHP Regular Array CRUD Operations Class

Share Button

<?php

/**
*   @Autor:     Kwacho - Michal Kwasniewski
*   @Website:   www.sharemyweb.com
*
*   NOTE: Feel free to edit, change and use this class...just DON'T FORGET TO LINK WEBPAGE.
*
*   WORKING WITH REGULAR ARRAYS : Basic CRUD Functionalities
*
*        CREATE entry:   add item/items to array
*        READ   entry:   display all items in array
*        UPDATE entry:   rename/update item in array
*        DELETE entry:   delete item/items from array
*
*/

// Class Start
class CRUD_Arrays{

   // Items Array with dummy data
   public static $itemsArray=array("one","two","three");

   // HELPER METHODS that will be necessary to work with main CRUD methods

   /* Checks if array is empty */
   private function isEmptyArray($array){
      if(empty($array))
      return false;
   }

   /* Check If item is in array */
   private function checkIfItemExist($item){
      if(in_array($item,self::$itemsArray)){
         return true;
      } else {return false;}
   }

   /*  Find an item and returns its corresponding index.
   *   If item is not in array, display apporpriate message
   */
   private function findItem($item){
      if($this->checkIfItemExist($item)){
         $this->getItem = array_search($item,self::$itemsArray);
         /* Return it as string, otherwise if array index resoves to 0,
         *  renameItem() method does not want to rename first/[0] value 
         */
         return "{$this->getItem}";
      }else{
         echo "There's no " . $item . " in array";
      }
   }

   //MAIN CRUD Functionalities

   /*  Add item/items to array. You can pass as many arguments as you wish.
   *    If any of the items already exists it will NOT be added again or ducplicated
   */
   public function addItems(){
      /**  Returns the number of arguments passed to the function */
      $numOfArgs = func_num_args();
      /** Returns an array with all arguments passed to the function */
      $listOfArgs = func_get_args();

      /** Add Items to array */
      for ($i = 0; $i < $numOfArgs; $i++) {
         array_push(self::$itemsArray,$listOfArgs[$i]);
      }
   }

   /* Loops through an array and dispalys all items */
   public function displayItems($items){
      foreach($items as $item){
         echo "<li>{$item}</li>";
      }
   }

   /* Renames/Changes/Updates an item to another item/item name */
   public function renameItem($item,$newItem){
      if($this->findItem($item) != null){
         self::$itemsArray["{$this->findItem($item)}"] = $newItem;
      }
   }

   /* Clear Array: a) $itemsArray = NULL, b) $itemsArray = array()*/
   public function clearArray(){
      //self::$itemsArray = NULL;
      self::$itemsArray = array();
   }

   /*  Deletes specific item/items from array.
   *   You can pass as many arguments as you wish.
   */
   public function deleteItems(){
      /**  Returns the number of arguments passed to the function */
      $numOfArgs = func_num_args();
      /** Returns an array with all arguments passed to the function */
      $listOfArgs = func_get_args();
      /** Delete items from array */
      for ($i = 0; $i < $numOfArgs; $i++) {
         if($this->checkIfItemExist($listOfArgs[$i])){
            $this->getItem = array_search($listOfArgs[$i],self::$itemsArray);
            unset(self::$itemsArray[$this->getItem]);
         }
      }
   }
}   // Class end

?>

 

EXAMPLE USE:

 

Firstly, let’s instantiate array so we can access all methods we want.

/* Instance of a CRUD_Arrays class */
$arrayClass = new CRUD_Arrays();

 

Secondly, we’re going to display all items. At the beginning we’ve put some dummy
data to array to have something to work with.
displayItems() method will loop through an array of items and display all of them.
It’s a raw output, but if you wish you can add some markup to it.

echo "<ul>" . $arrayClass->displayItems(CRUD_Arrays::$itemsArray) . "</ul>";

Screen Shot 2016-01-12 at 12.58.43

 

Now we’re going to add few items to array. Below method addItems() adds one item to array.

$arrayClass->addItems("four");

It’s possible to add more than one item to array at the same time using the same method as above.

$arrayClass->addItems("five","six","seven");

Screen Shot 2016-01-12 at 13.02.22

 

Now we can check how deleteItems() method works. Same as addItem() method, deleteItem() allows
to delete one or more arguments from array at the same time.

$arrayClass->deleteItems("one");

OR

$arrayClass->deleteItems("two","three");

Screen Shot 2016-01-12 at 13.02.42

 

Wen can also rename items. renameItem() method takes two arguments : item to be changes, new item. Simple right?

echo $arrayClass->renameItem("five","FIVE");
echo $arrayClass->renameItem("seven","SEVEN");

Screen Shot 2016-01-12 at 13.03.02

 

 

Just Copy Paste CRUD_Arrays class and have a go.

Share Button