Get the App
SLTechnology News&Howtos  ›  Development  › 

How php deletes the fifth element in an array

Shulou Source: shulou.com Published: 2022-05-31 17:57:03 10月03日 Update

Most people don't understand the knowledge points of this article "how to delete the 5th element in the array", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how to delete the 5th element in the array".

2 ways: 1, with array_splice(), just set the second parameter to 4, the third parameter to 1, syntax "array_splice($arr,4,1)". 2, with unset(), syntax "unset($arr[4])", applies to the index array, delete the element with index 4.

Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer

Php removes the fifth element from an array

Method 1: Use the array_splice() function

The array_splice() function deletes a specified number of array elements starting at a specified position.

Simply set the start position to 4 and the number of deletions to 1 to delete the fifth element (starting after the fourth element).

Examples:

Use the unset() function

If you want to delete an element from an array, you can use unset().

The unset() function allows you to remove elements from an array, but the array does not rebuild the index, that is, it keeps the original index, because the index in php has a special meaning.

Example: Delete the 5th element (element with index 4)

Description:

The array_splice() method works with indexed arrays and associative arrays.

The unset() method works with indexed arrays; in associative arrays, you need to get the string key of the fifth element first, and then delete the elements according to the key, but this is too cumbersome.

The above is about "php how to delete the fifth element in the array" this article content, I believe everyone has a certain understanding, I hope the content shared by Xiaobian is helpful to everyone, if you want to know more related knowledge content, please pay attention to the industry information channel.

Tags: Element array index content method function number location parameter article knowledge example article grammar association special value just meaning most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi Microsoft Xiaomi MySQL