This time to bring you a PHP delete array in the specified subscript element steps, the PHP delete array in the specified subscript element of attention to what, the following is the actual case, take a look.
1, before the array as the stack and the queue to delete the element operation, that is, in order to delete the regular. So, what if you need to remove an element from the middle of the array? You need the unset () function We're going to talk about today.
2, the unset () function allows you to cancel an element in an array, but the array does not rebuild the index, that is, keep the original index, because the index in PHP has a special meaning.
3. Example shows:
<?php $arr = Array (1=> ' one ',2=> ', ',3=> ' three '); Delete the element labeled 2 unset ($arr [2]);//will get Array (1=> ' one ',3=> ' three ') //Use Array_values () to re-index $aar = Array_values ($arr); $aar = Array (0=> ' one ',1=> ' three ')?>
4, the last sentence in the above example code to re-establish the array index, here I explain the following: Because after using the unset () function to delete an element, and did not re-establish index subscript order. If you need a sequential index subscript, you can use the Array_values () function to recreate the index subscript order.
Note: The re-indexing of the premises refers to the re-establishment of an index starting at 0, even if your index is not numerically named, and is re-indexed.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP replace article picture path to local server steps in detail
PHP's summary of source code encryption methods