How to delete an element from an array? Explain with example?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

we have soo many methods to delete an element from an array in php but the most common or mostly used methos are-

  • unset() = This function takes an element as a parameter and unset it. It wouldn’t change the keys of other elements.
  • array_splice() =This function takes parameters an array, offset[where to start] and length. It will automatically re-index an indexed array but not an associated array after deleting the elements.
  • array_diff() = This function takes an array and list of array values as input and deletes the giving values from an array.

example

<?php
 
 
 // with unset()
  $cities= ["bangalore","gwalior","delhi"];
  
  unset($cities[2]);
  
		echo "<pre>";
		
  print_r($cities);
  
		echo "</pre>";
		
		
// with array_splice()		
  $cities= ["bangalore","gwalior","delhi"];
  
  array_splice($cities,1,2);
  
		echo "<pre>";
		
  print_r($cities);
  
		echo "</pre>";
		
		
	// with array_diff()
  $cities= ["bangalore","gwalior","delhi"];
  
  $cities=array_diff($cities,["gwalior"]);
  
		echo "<pre>";
		
  print_r($cities);
  
		echo "</pre>";
			
		
?>

output

Array
(
    [0] => bangalore
    [1] => gwalior
)

Array
(
    [0] => bangalore
)

Array
(
    [0] => bangalore
    [2] => delhi
)

Related Posts

Modernizing Software Delivery in the Dutch Tech Landscape: A Pragmatic DevOps Playbook

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Finding Qualified AWS DevOps Trainers: Enterprise Playbook

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Teaching Failure: How to Build Incident Response and Resilience into DevOps Training

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

The Open Road in Japan: An Independent Rider’s Handbook for Cross-Country Touring

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

A Comprehensive Guide to Scooters, Motorcycles, and Road Trips

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Monitoring and Observability Training: Complete Curriculum Guide

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
guest

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x