EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP unset Array

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP unset Array

PHP unset Array

Introduction to PHP unset Array Function

In PHP programming language setting values to an array and un-setting it is very common. Un-setting an array means deleting the element(s) of an array. We can unset the value of a particular position of the array or the complete array.  There are various ways we can do this in the PHP language. Either we can achieve this by using our own custom code or using the PHP built-in function itself. While dealing with the array unset we should check the array of elements that are present there before printing that array. By doing so, we could be on the safer side as we will not see any message or the notice of the PHP warning.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

unset ($array1)

This will delete all the elements of an array $array1.

unset($array1[position]);

This will delete the element of an array $array1 by the position. The position is the array index always starts from 0.

How Does PHP unset Array work?

To make the un-setting array functional we need to have an array with some value on that. Let’s say, we have an array named $array1 with some value. Now we need to make this array empty, we can do this by using the PHP unset() function. We can also delete an array element by using the PHP unset feature.

We can do the below mentioned in the PHP unset array functionalities:

  • Unset an array.
  • Unset an element of an array with its index.
  • Unset array by its value – this can’t be directly achieved. The unset index will do the job here as well after searching the correct index of that element.

Examples to Implement of PHP unset Array

Below are the examples of PHP unset Array:

Example #1

In this example, we will declare an array with some values and print those array elements using the print_r function. After this, we will unset that array and try printing to see how the code behaves.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Array unset in PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php
$array1 = array(1 => "Red", 3=>"Green", 2=>"Blue");
echo "<pre>";
echo "Array elements are:<br>";
print_r($array1);
unset($array1); // unset the complete array.
print_r($array1); // this line give notice as we have unset the $array1 before printing.
?>
</body>
</html>

Output:

PHP unset Array Example 1

The warning is coming because we don’t have that array reference after unset. So, in this, we should not print array without checking if that array is existing or not.

Example #2

In this example, we will try to remove the above notice that is coming after the array reset. Checking array is existing or not is always a good practice to check that array or that value printing. So, in this example code, we will try to remove that notice message.

Code:

<?php
$array1 = array(1 => "Red", 3=>"Green", 2=>"Blue");
echo "<pre>";
echo "Array elements are:<br>";
print_r($array1);
unset($array1); // unset the complete array.
if(isset($array1)){
print_r($array1); // this line give notice as we have unset the $array1 before printing.
}
?>

Output:

PHP unset Array Example 2

Yes, we can see if(isset($array1)){ } do the trick for us in removing that notice message.

Example #3

Now let’s unset some elements of an array rather un-setting the whole array.

Code:

<?php
$array1 = array(1, 2, 33, 8, 9, 10);
echo "Array elements are:<br>";
echo "<pre>";
print_r($array1);
unset($array1[3]); // Unset the element of array that is on 4th index.
echo "Array elements after unset:<br>";
print_r($array1);
?>

Output:

ArrayElements Example 3

As we can see the above example code will delete the value of index 3 and the next value has been shifted to the 3rd and the same shifting for the other elements after index 3rd.

Example #4

Removing an array element by its value. This can be done directly, in this deletion process, first, we have to find out the position of that element then we can delete that element by using the unset() function by passing the position as a parameter.

Code:

<?php
$array1 = array(1, 2, 33, 8, 9, 10);
echo "Array elements are:<br>";
echo "<pre>";
print_r($array1);
if (($key = array_search(9, $array1)) !== false) { // if key exist
unset($array1[$key]); // unsetting that key
}
echo "Array elements after unset:<br>";
print_r($array1);
?>

Output:

ArrayElements Example 4

As we can see the 9 is that element we are trying the delete from that array. This process will begin from searching that specified element in the array if found then the unset will be executed for that finding index. Again here we must use the unset if the element is present in the array, if we are not doing so it will give a notice message, it can also delete other elements of that position.

Conclusion

In PHP language the unset of an array can be performed to a complete array or to the specific position of that array. There is no functionality to delete the array by its value directly but we can achieve the same by finding the element in the array first then the deletion can be performed on that array position. A developer or the coder must follow good coding practices while dealing with the un-setting array or the array element. We should also use condition or the try-catch block to handle the unwanted notice on the result area.

Recommended Article

This is a guide to the PHP unset Array. Here we discuss the Introduction to PHP unset Array Function and how it works along with examples as well as Code Implementation. You can also go through our other suggested articles to learn more-

  1. Socket Programming in PHP
  2. PHP Frameworks
  3. PHP POST Method

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More