Home arrow PHP arrow Page 6 - Arrays

Other Useful Array Functions - PHP

Arrays offer an ideal means for storing, manipulating, sorting, and retrieving the kinds of data sets that programmers spend a lot of time working with. PHP supports the array data type. This article explains the array-based features and functions it supports. It is excerpted from the book, Beginning PHP 5 and MySQL: From Novice to Professional, by W. Jason Gilmore (Apress, 2004; ISBN: 1893115518).

  1. Arrays
  2. Outputting Arrays
  3. Testing for an array
  4. Locating Array Elements
  5. Determining Array Size and Uniqueness
  6. Other Useful Array Functions
By: Apress Publishing
Rating: starstarstarstarstar / 37
June 23, 2005

print this article



In this section I introduce a number of array functions that perhaps donít easily fall into one of the prior sections, but are nonetheless quite useful.


mixed array_rand(array input_array [, int num_entries])

The array_rand() function will return one or more keys found in input_array. If you omit the optional num_entries parameter, only one random value will be returned. If num_entries is set to greater than one, that many keys will be returned. An example follows:

$states = array("Ohio" => "Columbus", "Iowa" => "Des Moines","Arizona" => "Phoenix");
$randomStates = array_rand($states, 2);

This returns:

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


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

The array_chunk() function breaks input_array into a multidimensional array consisting of several smaller arrays consisting of size elements. 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",
// shuffle the cards
// Use array_chunk() to divide the cards into four equal "hands"
$hands = array_chunk($cards, 4);

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 ) )============================================================


Arrays play an indispensable role in programming, and are ubiquitous in every imaginable type of application, Web-based or not. The purpose of this chapter was to bring you up to speed regarding many of the PHP functions that will make your programming life much easier as you deal with these arrays.

The next chapter focuses on yet another very important topic: object-oriented programming. This topic has a particularly special role in PHP 5, because the process has been entirely redesigned for this major release.

>>> More PHP Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: