When you incorporate arrays into your application, you’ll sometimes need to know whether a particular variable is an array. A built-in function, is_array(), is available for accomplishing this task. is_array() boolean is_array(mixed variable) The is_array() function determines whether variable is an array, returning TRUE if it is, and FALSE otherwise. Note that even an array consisting of a single value will still be considered an array. An example follows: $states = array("Florida"); The results are: ==================================================== Adding and Removing Array Elements PHP provides a number of functions for both growing and shrinking an array. Some of these functions are provided as a convenience to programmers who wish to mimic various queue implementations (FIFO, LIFO, and so on), as reflected by their names (push, pop, shift, and unshift). Even if you don’t know what queue implementations are, don’t worry; these functions are easy to use, and examples are provided for each.
$arrayname[] This isn’t a function, but a language feature. You can add array elements simply by executing the assignment, like so: $states["Ohio"] = "March 1, 1803"; In the case of a numerical index, you can append a new element like this: $state[] = "Ohio"; Sometimes, however, you’ll require a somewhat more sophisticated means for adding array elements (and subtracting array elements, a feature not readily available in the fashion described for adding elements). These functions are introduced throughout the remainder of this section. array_push() int array_push(array target_array, mixed variable [, mixed variable...]) The array_push() function adds variable onto the end of the target_array, returning TRUE on success and FALSE otherwise. You can push multiple variables onto the array simultaneously, by passing these variables into the function as input parameters. An example follows: $states = array("Ohio","New York"); array_pop() mixed array_pop(array target_array) The array_pop() function returns the last element from target_array, resetting the array pointer upon completion. An example follows: $states = array("Ohio","New York","California","Texas"); $state = array_pop($states); // $state = "Texas"
blog comments powered by Disqus |
|
|
|
|
|
|
|