How to delete element re-indexing in php array, php array element indexing _ PHP Tutorial

Source: Internet
Author: User
The method for deleting the element re-indexing in the php array is the php array element index. The method of deleting element re-indexing in the php array. if you want to delete an element in an array, you can directly use the unset, however, what I saw today has allowed me to delete the element re-indexing method from the large php array, and the php array element indexing method.

If you want to delete an element from an array, you can use the unset directly, but what you see today surprised me.

The code is as follows:


<? Php
$ Arr = array ('A', 'B', 'C', 'D ');
Unset ($ arr [1]);
Print_r ($ arr );
?>


After print_r ($ arr), the result is not that. The final result is Array ([0] => a [2] => c [3] => d)

So how can we fill in the missing elements and re-index the array? The answer is:

Array_splice ():

The code is as follows:


<? Php
$ Arr = array ('A', 'B', 'C', 'D ');
Array_splice ($ arr, 1, 1 );
Print_r ($ arr );
?>

After print_r ($ arr), the result is A (www.jb51.net) rray ([0] => a [1] => c [2] => d)

Deletes the specified element of an array.

Array_search () is more practical.

The array_search () function is the same as the in_array () function. you can find a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned.

The code is as follows:


$ Array = array ('1', '2', '3', '4', '5 ');
$ Del_value = 3;
Unset ($ array [array_search ($ del_value, $ array)]); // use unset to delete this element
Print_r ($ array );


Output
Array ('1', '2', '4', '5 ');

However, if you want to re-index the array, you need to use foreach to traverse the deleted array and re-create an array.


How can I delete an array key value in php to make it an index array?

Array_values () returns all values in the input array and creates a digital index for them.

Array_value ($ arr ['Date']);

After php deletes a specified item in a two-dimensional array, the indexes are still arranged in order?

Solution 1: Create an array:

$ NewArr = array ();

Foreach ($ arr as $ key => $ value ){
$ NewArr [] = $ value;

}

Solution 2: Sorting
Sort ($ arr );

If you want to delete an element from an array, you can use the unset directly, but what you see today makes me large...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.