DSA - PHP Array using Oops Series-1
Here We are going to perform all these Array Operation In PHP using Oops
-
Traverse − print all the array elements one by one.
-
Insertion − Adds an element at the given index.
-
Deletion − Deletes an element at the given index.
-
Update − Updates an element at the given index.
1. Traverse -- Array With PHP.
Following is sample PHP code.
public function Traverse(){
echo 'Traverse Array ::';
foreach($this->arr as $key=>$val)
echo "\n ".$key.':'.$val;
}
2. Insertion -- Array With PHP.
Following is sample PHP code.
public function Insertion($item,$positon){
$lenArray = count($this->arr)-1;
$itemPosition = $positon - 1;
if($itemPosition > $lenArray){
$this->arr[$lenArray + 1] = $item;
}else{
$i = $lenArray;
while($i>=$itemPosition){
$i==$itemPosition ? $this->arr[$i+1] = $item:$this->arr[$i+1] = $this->arr[$i];
$i--;
}
}
}
2. Deletion -- Array With PHP.
Following is sample PHP code for Deletion.
public function Deletion($positon){
$lenArray = count($this->arr)-1;
if($lenArray <$lenArray){
return 'You have enter wrong Index';
}else{
while ($lenArray > $positon){
$this->arr[$positon] = $this->arr[$positon+1];
$positon++;
}
array_pop($this->arr);
}
}
2. Update -- Array With PHP.
Following is sample PHP code for Update.
public function Updates($item,$positon){
count($this->arr)<($positon - 1)?$this->arr[count($this->arr)]=$item:$this->arr[$positon]=$item;
}
Here is all the Code of with class.
/*
------------ Here We are going to perform all these Array Operation ------------
Traverse − print all the array elements one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Update − Updates an element at the given index.
*/
//Class Start here
class ArrayOeration{
public $arr;
function __destruct() {
echo "\n \n Final OutPut of Array \n \n";
print_r($this->arr);
}
// ****** Traverse − print all the array elements one by one. ****** \\
public function Traverse(){
echo 'Traverse Array ::';
foreach($this->arr as $key=>$val)
echo "\n ".$key.':'.$val;
}
// ****** Insertion − Adds an element at the given index. ****** \\
public function Insertion($item,$positon){
$lenArray = count($this->arr)-1;
$itemPosition = $positon - 1;
if($itemPosition > $lenArray){
$this->arr[$lenArray + 1] = $item;
}else{
$i = $lenArray;
while($i>=$itemPosition){
$i==$itemPosition ? $this->arr[$i+1] = $item:$this->arr[$i+1] = $this->arr[$i];
$i--;
}
}
//print_r($this->arr);
}
// ****** Update − Updates an element at the given index. ****** \\
public function Updates($item,$positon){
count($this->arr)<($positon - 1)?$this->arr[count($this->arr)]=$item:$this->arr[$positon]=$item;
// print_r($this->arr);
}
// ****** Deletion − Deletes an element at the given index. ****** \\
public function Deletion($positon){
$lenArray = count($this->arr)-1;
if($lenArray <$lenArray){
return 'You have enter wrong Index';
}else{
while ($lenArray > $positon){
$this->arr[$positon] = $this->arr[$positon+1];
$positon++;
}
array_pop($this->arr);
}
//print_r($this->arr);die;
}
}// Class End here
$arr= array('jfjd',434,'Hello','Vishwakirti sharma');
$ArrOps = new ArrayOeration; // Call Operation
$ArrOps->arr = $arr;
$ArrOps->Traverse();
$ArrOps->Insertion('insert',0); // Item,Index of insert
$ArrOps->Updates('update',3); // Item,Index of insert
$ArrOps->Deletion(1); // Index