PHP unset Array

WBOY
Release: 2024-08-29 12:44:58
Original
537 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:

   Array unset in PHP   
"Red", 3=>"Green", 2=>"Blue"); echo "
"; echo "Array elements are:
"; print_r($array1); unset($array1); // unset the complete array. print_r($array1); // this line give notice as we have unset the $array1 before printing. ?>
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:

 "Red", 3=>"Green", 2=>"Blue"); echo "
"; echo "Array elements are:
"; 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:

"; echo "
"; print_r($array1); unset($array1[3]); // Unset the element of array that is on 4th index. echo "Array elements after unset:
"; 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:

"; echo "
"; print_r($array1); if (($key = array_search(9, $array1)) !== false) { // if key exist unset($array1[$key]); // unsetting that key } echo "Array elements after unset:
"; 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.

결론

PHP 언어에서는 배열의 설정 해제를 전체 배열 또는 해당 배열의 특정 위치에 수행할 수 있습니다. 해당 값으로 배열을 직접 삭제하는 기능은 없지만 배열에서 요소를 먼저 찾은 다음 해당 배열 위치에서 삭제를 수행하여 동일한 결과를 얻을 수 있습니다. 개발자나 코더는 설정되지 않은 배열이나 배열 요소를 처리하는 동안 좋은 코딩 방법을 따라야 합니다. 또한 결과 영역에서 원치 않는 알림을 처리하려면 조건 또는 try-catch 블록을 사용해야 합니다.

추천기사

PHP unset Array에 대한 안내입니다. 여기서는 예제 및 코드 구현과 함께 PHP 설정되지 않은 배열 함수의 소개, 구문 및 작동에 대해 설명합니다. 다른 추천 기사를 통해 자세히 알아볼 수도 있습니다.

  1. PHP의 소켓 프로그래밍
  2. PHP 프레임워크
  3. PHP POST 방식

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!