PHP provides several basic array-based functions like range(), count(), foreach(), in_array(), shuffle(), extract(), and compact(). Here is their signature:
$new_array = range($n1, $n2); $num_of_elements = count($array); $boolean_is_found = in_array($value, $array); shuffle($array); $num_variables = extract($array, $prefix_flag, $prefix_tag); $new_array = compact($var1, $var2, $var3, ... );
Let us briefly describe these functions.
range() takes two numbers and returns an array with elements containing numbers lying between these numbers, including them.
count() returns the total number of elements present in an array.
in_array(), searches a passed value in the passed array -- if it finds the value, then it returns true, otherwise it returns false.
shuffle() takes an existing array and shuffles its elements.
extract() defines variables with names as the keys of the associative arrays and it returns the number of variables created successfully. This function also takes two optional arguments of prefix flag and prefix tag. extract() accepts several types of flags, some of which are: EXTR_PREFIX_ALL, EXTR_PREFIX_SAME, EXTR_OVERWRITE, and EXTR_SKIP. The flag EXTR_PREFIX_ALL indicates extract() to add the passed prefix tag as a prefix to variables names. If a variable with prefixed name exists, then extract simply overwrites it.
For extract(), there might be cases when we would like to prefix the variable name only if a variable with the same name (before adding the prefix) already exists. In such cases, we can use flag EXTR_PREFIX_SAME. EXTR_OVERWRITE indicates that if the variable (with the same name) exists, then simply overwrite it. If no flag is specified, then extract() assumes a default behavior of EXTR_OVERWRITE. On the contrary, EXTR_SKIP indicates that if a variable (with the same name) exists then do not overwrite it.
compact() does the exact opposite of extract -- it takes a set of variable names (name of variables and not the variables themselves!) and builds an associative array from them, where the variables become the keys of the array and their values become their values in the array.
The combination of range() and shuffle() can be very handy in certain cases. Often we need a set of unique random numbers lying between two values. With PHP, the easiest way to do this would be to create an array using the range() and then randomize the array using shuffle(). This approach not only ensures that these numbers are random, but also ensures that if we were to access the array one by one (let us say by accessing them in sequence), then we would see each number exactly once even though the entire series is random!
Let us go through two simple programs to understand the usage of these functions.
The first program (provided below) demonstrates range(), count(), shuffle(), and in_array() functions.
<?php $var_array = range(100, 110); $len_array = count($var_array); echo "The total length of the array is $len_array <br>"; echo "The elements are: <br>"; foreach ($var_array as $element) { echo "$element "; } shuffle($var_array); echo "<br>The shuffled elements are: <br>"; foreach ($var_array as $element) { echo "$element "; } /* Demos usage of in_array() */ $found101 = in_array(101, $var_array); $found237 = in_array(237, $var_array); echo "<br>found101: "; var_dump($found101); echo "<br>found237: "; var_dump($found237); ?>
The output (provided below) shows that range() generates an array with numbers from 100 to 110 and includes both 100 and 110. PHP would not complain if we do not pass the two numbers in an increasing order i.e. second number is larger than the first number. Thus, if we were to call range(110,100), we would still get the same numbers, but, PHP would (cleverly) return these numbers in reverse order i.e 110, 109, 108, and so on! After generating these numbers, the program uses shuffle() to randomize elements of $var_array. Lastly, as expected, in_array() returns true, if the value is present; else, it returns false.
The total length of the array is 11 The elements are: 100 101 102 103 104 105 106 107 108 109 110 The shuffled elements are: 109 107 105 106 110 104 103 101 100 102 108 found101: bool(true) found237: bool(false)
The second program focuses on extract() and compact(). For extract(), this program demonstrates two flags: EXTR_PREFIX_ALL and EXTR_PREFIX_SAME.
<?php $user_array = array('name' => "jwayne", 'city' => "Palo Alto"); /* Show usage of extract() */ $ret_extract = extract($user_array); echo "Number of extracted variables is: $ret_extract <br>"; echo "Variable name: $name <br>"; echo "Variable city: $city <br>"; /* Calling extract() with EXTR_PREFIX_ALL adds prefixes to all variables */ $ret_extract = extract($user_array, EXTR_PREFIX_ALL, "my"); echo "<br>Number of extracted variables (prefix EXTR_PREFIX_ALL) is: $ret_extract <br>"; echo "Variable my_name: $my_name <br>"; echo "Variable my_city: $my_city <br>"; /* Calling extract() with EXTR_PREFIX_SAME selectively adds the prefix */ unset($city); $ret_extract = extract($user_array, EXTR_PREFIX_SAME, "user"); echo "<br>Number of extracted variables (prefix EXTR_PREFIX_SAME) is: $ret_extract <br>"; echo "Variable name: $name <br>"; echo "Variable city: $city <br>"; echo "Variable user_name: $user_name <br>"; echo "Variable user_city (should be NULL): $user_city <br><br>"; /* Show usage of compact() */ $status = "Online"; $num_photos = 210; $num_videos = 10; $compact_array = compact("status", "num_photos", "num_videos"); print_r($compact_array); echo "<br>"; /* The following would not work */ $compact_array_incorrect = compact($status, $num_photos, $num_videos); print_r($compact_array_incorrect); ?>
We provide the output of this program below. As expected, the extract() call with EXTR_PREFIX_ALL creates $my_name and $my_city. Even if we had defined these variables ($my_name and $my_city) prior to calling extract(), then EXTR_PREFIX_ALL would have simply overwritten them! However, after the first call, $name, and $city are already defined. So, to demonstrate extract() with EXTR_PREFIX_SAME, we un-define $city variable. With this, the extract() call adds suffix only for the $name variable and creates $city without adding any prefix.
Number of extracted variables is: 2 Variable name: jwayne Variable city: Palo Alto Number of extracted variables (prefix EXTR_PREFIX_ALL) is: 2 Variable my_name: jwayne Variable my_city: Palo Alto Number of extracted variables (prefix EXTR_PREFIX_SAME) is: 2 Variable name: jwayne Variable city: Palo Alto Variable user_name: jwayne Variable user_city (should be NULL): Array ( [status] => Online [num_photos] => 210 [num_videos] => 10 ) Array ( )
Please note that for compact(), we must pass variable names and not their values (i.e. pass "status" instead of $status). When we incorrectly pass variables value instead of their names, above compact() fails to create a new array.