PHP unset Array

WBOY
Release: 2024-08-29 12:44:58
Original
740 people have browsed it

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 notice of the PHP warning.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

unset ($array1)
Copy after login

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

unset($array1[position]);
Copy after login

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.

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 class="brush:php;toolbar:false">";
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>
Copy after login

Output:

PHP unset Array

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 class="brush:php;toolbar:false">";
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.
}
?>
Copy after login

Output:

PHP unset Array

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 class="brush:php;toolbar:false">";
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);
?>
Copy after login

Output:

PHP unset Array

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 class="brush:php;toolbar:false">";
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);
?>
Copy after login

Output:

PHP unset Array

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, syntax, and working of PHP unset Array function 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

The above is the detailed content of PHP unset Array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template