Searching...
Thursday 18 July 2013

Other Useful Array Functions


This article introduces a number of array (what is an array) functions (more about functions in php) that perhaps don’t easily fall into one of the prior articles but are nonetheless quite useful.

Returning a Random Set of Keys

The array_rand() function will return a random number of keys found in an array. Its prototype follows:

mixed array_rand(array array [, int num_entries])

If you omit the optional num_entries parameter, only one random value will be returned. You can tweak the number of returned random values by setting num_entries accordingly. An example follows:

$states = array("Ohio" => "Columbus", "Iowa" => "Des Moines",
"Arizona" => "Phoenix");
$randomStates = array_rand($states, 2);
print_r($randomStates);
This returns the following (your output may vary):

Array ( [0] => Arizona [1] => Ohio )

 

Shuffling Array Elements

The shuffle() function randomly reorders an array. Its prototype follows:

void shuffle(array input_array)

Consider an array containing values representing playing cards:

$cards = array("jh", "js", "jd", "jc", "qh", "qs", "qd", "qc",
"kh", "ks", "kd", "kc", "ah", "as", "ad", "ac");
shuffle($cards);
print_r($positions);

This returns something along the lines of the following (your results will vary because of the shuffle):

Array ( [0] => js [1] => ks [2] => kh [3] => jd
[4] => ad [5] => qd [6] => qc [7] => ah
[8] => kc [9] => qh [10] => kd [11] => as
[12] => ac [13] => jc [14] => jh [15] => qs )

Adding Array Values

The array_sum() function adds all the values of input_array together, returning the final sum. Its prototype follows:

mixed array_sum(array array)

If other data types (a string, for example) are found in the array, they will be ignored. An example follows:

<?php
$grades = array(42, "hello", 42);
$total = array_sum($grades);
print $total;
?>

This returns the following:

84

Subdividing an Array

The array_chunk() function breaks input_array into a multidimensional array that includes several smaller arrays consisting of size elements. Its prototype follows:

array array_chunk(array array, int size [, boolean preserve_keys])

If the input_array can’t be evenly divided by size, the last array will consist of fewer than size elements. Enabling the optional parameter preserve_keys will preserve each value’s corresponding key. Omitting or disabling this parameter results in numerical indexing starting from zero for each array. An example follows:

$cards = array("jh", "js", "jd", "jc", "qh", "qs", "qd", "qc",
"kh", "ks", "kd", "kc", "ah", "as", "ad", "ac");
// shuffle the cards
shuffle($cards);
// Use array_chunk() to divide the cards into four equal "hands"
$hands = array_chunk($cards, 4);
print_r($hands);

This returns the following (your results will vary because of the shuffle):

Array ( [0] => Array ( [0] => jc [1] => ks [2] => js [3] => qd )
[1] => Array ( [0] => kh [1] => qh [2] => jd [3] => kd )
[2] => Array ( [0] => jh [1] => kc [2] => ac [3] => as )
[3] => Array ( [0] => ad [1] => ah [2] => qc [3] => qs ) )


0 comments:

Post a Comment

 
Back to top!