For (old Array) {if (the value to be deleted) continue $ newArr [] each value} return $ newArr instance: Array ([0] & gt; hello [1] & gt; world [2] & gt; It & amp; 39; s [3] & gt; a [4] & gt; bea
- For (old array .....){
- If (the value to be deleted)
- Continue
- $ NewArr [] = each value
- }
- Return $ newArr
Instance:
- Array ([0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day .)
- Foreach ($ array as $ k => $ v ){
- If ($ v = 'day '){
- Unset ($ array [$ k]):
- }
- }
Using foreach is inefficient. check the built-in functions in php:
$ Arr = array ("Hello", "world", "It's", "beautiful", "day ");
Instance 1:
- $ Arr = array_flip ($ arr );
- Unset ($ arr ['world']);
- $ Arr = array_flip ($ arr );
- Print_r ($ arr );
Example 2:
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.
- $ Arr = Array ([0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day .);
- If ($ key = array_search ('day', $ arr ))){
- Unset ($ arr [$ key]);
- }
Example 3:
The array_splice () function is similar to the array_slice () function. it selects a series of elements in the array but does not return them. Instead, it deletes them and replaces them with other values.
- If ($ key = array_search ('day', $ arr ))){
- Array_splice ($ arr, $ key, 1 );
- }