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

Mastering Data Pipelines: The Complete Career Guide to CDOM – Certified DataOps Manager

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

Mastering the Future of AI Production: The Certified MLOps Manager 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

Mastering the Machine Learning Lifecycle: A Professional Guide to the Certified MLOps Architect Certification

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 Certified MLOps Professional Path: A Definitive Guide for Modern Engineering Careers

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

Mastering the Certified MLOps Engineer Path: Skills, ROI, and Career Impact

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

Certified AIOps Professional: A Comprehensive Guide Value, Skills, and Career

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
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x