Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

srand(time());
for($i=0; $i<10; $i++)
{
$data[] = rand(0,1);
}


//print out the keys to 1's
foreach(array_keys($data, 1) as $key)
{
print("$key
\n");
}
?>


array array_merge (array data, array data, ...)


The array_merge function takes two or more arrays and returns a single array containing
all elements. Elements indexed by integers are added to the new array one at a time, in
most cases renumbering them. Elements indexed by strings retain their index values and
are added as they are encountered in the input arrays. They may replace previous values.
If you are unsure of the indices used in the merged arrays, you can use array_values to
make sure all values are indexed by an integer.


<?
function printElement($element)
{
print("$element
\n");
}


//set up an array of color names
$colors = array("red", "blue", "green");
$more_colors = array("yellow", "purple", "orange");


//merge arrays
$all_colors = array_merge($colors, $more_colors);


//print out all the values
array_walk($all_colors, "printElement");
?>


boolean array_multisort(array data, integer direction, ...)


The array_multisort function sorts arrays together, as if array were a column in a
table. The data argument is an array and the direction argument is one of two
constants: SORT_ASC or SORT_DESC. These stand for ascending and descending,
respectively. If left out, the direction defaults to ascending order, which is smallest to
largest. You may specify any number of arrays, but you must alternate between arrays
and sort order constants as you do.

Free download pdf