- $arr = Array (' A ', ' B ', ' C ', ' d ');
- Unset ($arr [1]);
- Print_r ($arr);
- ?>
Copy CodePrint_r ($arr), the result is not that, the end result is an Array ([0] = a [2] = c [3] + D) How do I let the missing elements be filled and the array will be re-indexed? Using Array_splice ():
- $arr = Array (' A ', ' B ', ' C ', ' d ');
- Array_splice ($arr, 1, 1);
- Print_r ($arr);
- ?>
- Print_r ($arr), the result is a (bbs.it-home.org) Rray ([0] = a [1] = + c [2] + D)
Copy Code2, delete array specified element Array_search () the utility Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns false if not found
- $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);
Copy CodeOutput Array (' 1 ', ' 2 ', ' 4 ', ' 5 '); If you want to re-index an array, you need to re-establish an array after using the foreach traversal of the deleted array. |