Php two ways to delete array elements unset, array_splice usage difference
- $ Arr = array ('A', 'B', 'C', 'D ');
- Unset ($ arr [1]);
- Print_r ($ arr );
- ?>
Result: Array ([0] => a [2] => c [3] => d) how to fill in the missing elements and re-index the Array? The answer is array_splice (): Example:
- $ Arr = array ('A', 'B', 'C', 'D ');
- Array_splice ($ arr, 1, 1 );
- Print_r ($ arr );
- ?>
-
Result: Array ([0] => a [1] => c [2] => d) delete a specific element in the Array (bbs.it-home.org script School ):
- $ Arr2 = array (1, 3, 5, 7, 8 );
- Foreach ($ arr2 as $ key => $ value)
- {
- If ($ value = 3)
- Unset ($ arr2 [$ key]);
- }
- Var_dump ($ arr2 );
- ?>
Delete an empty array:
- $ Array = ('a' => "abc", 'B' => "bcd", 'C' => "cde", 'D' => "def ", 'E' => "");
- Array_filter ($ array );
- Echo"
"; - print_r($array);
- ?>
Result: Array ([a] => abc=> Bcd [c] => cde [d] => def) Conclusion: If the array_splice () function is deleted, the index value of the array also changes. If the unset () function is deleted, the index value of the array remains unchanged. |