- Chapter 3: Using Arrays
- 1 Declaring an Array
- 2 Printing Out an Array
- 3 Eliminating Duplicate Elements
- 4 Enlarging or Shrinking an Array
- 5 Merging Arrays
- 6 Iteratively Processing Elements in an Array
- 7 Accessing the Current Element in an Array
- 8 Accessing Different Areas of an Array
- 9 Searching an Array
- 10 Searching for the Different Elements in Two Arrays
- 11 Randomizing the Elements of an Array
- 12 Determining the Union, Intersection, or Difference Between Two Arrays
- 13 Sorting an Array
- 14 Sorting Sensibly
- 15 Reversing Order
- 16 Perl-Based Array Manipulation Features
3.6 Iteratively Processing Elements in an Array
You want to perform an action on each element of an array.
Technique
Take a stroll through the array using array_walk():
<?php function ascii_codes (&$element) { $element = ord ($element); } $nice_line = "What Up everyone"; $chars = preg_split ("//", $nice_line); array_walk ($chars, 'ascii_codes'); ?>
Or use a for loop to loop through the array:
<?php function ascii_codes ($element) { $element = ord ($element); } $nice_line = "What Up everyone"; $chars = preg_split ("//", $nice_line); for ($i = 0; $i < count ($chars); $i++) { $chars[$i] = ascii_codes ($chars[$i]); } ?>
Comments
The array_walk() function invokes a user-defined function on each element of the passed array. The elements are passed as the first argument of the specified function; if the function requires more than one argument, a warning will be generated each time array_walk() calls the function. These warnings can be suppressed by prepending the '@' sign to the array_walk() call, or by using the error_reporting() function (setting it to E_ERROR). By default, the elements are passed to the user function by value. If you prepend & to the declaration, the elements will be passed by reference and you will be able to modify them inside the function.
For Perl programmers, it might be helpful to think of array_walk() as the map() function, where the following:
@in = map { $_ * $_ } @in;
translates to
<?php function square (&$array_element) { $array_element = $array_element * $array_element; } array_walk ($in, 'square'); ?>
If you want to use array_walk() but don't want to predeclare a function every time you want to loop over an array, you can use PHP's create_function() function and loop over the array:
<?php $ar = array ("hello", "world"); array_walk ($ar, create_function ('&$word', '$word = strtolower($word);')); ?>
You can also pass an array by reference and then work with that array.
Unlike some other languages such as Perl and C, in PHP, references do not need to be dereferenced. Therefore, the following will work:
<?php // This function trims message board topics down to 40 characters // and makes sure that ... occurs after a word boundary function trim_topics (&$topics) { for ($c = 0; $c < count ($topics); $c++) { if (strlen ($topics[$c]) > 40) { -$topics[$c] = preg_replace ("/\s+(\S+)?$/","...", substr
($topics[$c], 0, 40)); } } } trim_topics ($msg_topics); ?>
References in PHP are simply symbol table aliases, nothing more. That means when you declare a reference to a variable, $var1, you are telling PHP that the reference points to the same data to which $var1 points. If it helps, you can think of references in PHP as similar to hardlinks on the UNIX filesystem.
If you want to modify the data inside a function and have it changed in the outer scope as well, in C you would pass the pointer to the data to the function. In PHP, you declare that the function accepts a reference and simply pass the data. In fact, it's good programming practice not to use reference passing at the call time; it's much cleaner to do it in function declaration.
You can create a reference to an existing PHP variable by sticking an & sign before the variable, like so:
<?php $algernon = "Relations are simply a tedious pack of people, who haven't got the remotest knowledge of how to live, nor the slightest instinct about when to die." // Oscar Wilde, The Importance of Being Earnest $bumblebury = &$algernon; ?>
$bumblebury and $algernon now point to the same data. Performing some sort of operation on either variable will change the value of the other one as well.
Gotcha
In general, when looking to optimize your PHP code, don't worry too much about references; it is most often faster simply to let Zend handle such matters. One area however where references can pay off is when using objects, where you should always pass references.