array_walk() function applies a function to several or all elements in an array. : array_walk « Data Structure « PHP






array_walk() function applies a function to several or all elements in an array.

 
Its syntax is: int array_walk(array array, string func_name, [mixed data])


<?
//Using array_walk() to delete duplicates in an array:

    function delete_dupes($element) {
         static $last="";
         if ($element == $last)
              unset($element);
         else
              $last=$element;
    }
    
    $emails = array("b@b.com", "c@w.com", "b@b.com");
    
    sort($emails);
    print_r($emails);
    echo '<br>';
    reset($emails);
    print_r($emails);
    echo '<br>';
    array_walk($emails,"delete_dupes");
    print_r($emails);
    echo '<br>';
    
?>
  
  








Related examples in the same category

1.Applying Functions to Array Elements Using array_walk()
2.Apply a user function to every member of an array.