Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

Understanding Arrays

Arrays are used to contain and compute multiple pieces of data recursively or by targeting them directly. The comma separated pieces of data inside of arrays are called "elements". Each element has a "key" which is used to access or manipulate the element and its value. Keys may be explicitly assigned to form associative arrays. If keys are not assigned by the author, the array will have automatically assigned numeric keys starting with the number zero(0).

Array Syntax Basics

Create a new empty array to populate with data later in the script. $cart = array(); Target keys in the array to update or insert data at those element indexes. $cart[0] = "eggs"; $cart[1] = "milk"; $cart[2] = "cereal"; Populate an array with data upon creation. $cart = array("eggs", "milk", "cereal"); Access array elements by their numeric indexes. echo $cart[0]; /* eggs */ echo $cart[1]; /* milk */ echo $cart[2]; /* cereal */ Square brackets or curly braces could be used to access array keys in PHP. echo $cart[0]; /* eggs */ echo $cart{1}; /* milk */ Append more elements onto the end of an array. $arr = array(10,20,30); $arr[] = 40; $arr[] = 50; Arrays can contain mixed data types(strings, numbers, booleans, arrays, object, etc...). $arr = array('Joe', 23 , 'USA', true); Serialize an array which generates a storable representation of the array. Array objects cannot be directly stored without first converting the array to a string based representation of the array. They can be converted back to array objects later using the unserialize() function. echo serialize($arr); /* a:4:{i:0;s:3:"Joe";i:1;i:23;i:2;s:3:"USA";i:3;b:1;} */ Print an array. The print_r() function prints human-readable information about a variable. print_r($arr); /* Array ( [0] => Joe [1] => 23 [2] => USA [3] => 1 ) 1 */ Loop over a basic array to access all elements recursively. $weapons = array("Sword", "Dagger", "Bow", "Slingshot"); for( $i=0; $i < count($weapons); $i++ ){ echo '<h4>Element index '.$i.' - '.$weapons[$i].'</h4>'; }

Associative Arrays

Associative arrays are similar to basic arrays except that each element key is explicitly named or numbered. $cart = array( "bananas" => 6, "milk" => 1, "cereal" => 2 ); /* access bananas value */ echo $cart["bananas"]; /* update bananas value */ $cart["bananas"] = 10; /* loop over and process the whole cart array */ foreach( $cart as $key => $value ){ echo '<h4>'.$key.' - '.$value.'</h4>'; } In this example we are assigning numeric keys to the array elements. $arr = array( 7 => "a", 25 => "b", 8 => "c" ); foreach( $arr as $key => $value ){ echo '<h4>'.$key.' - '.$value.'</h4>'; }

Multidimensional Arrays

Multidimensional arrays are arrays that contain multiple nested arrays at varying depths.

Pack several basic arrays into an array container $arr1 = array(1,2,3); $arr2 = array(4,5,6); $arr3 = array(7,8,9,10); $data = array($arr1, $arr2, $arr3); echo $data[2][1]; Create a two-dimensional array holding multiple associative arrays. <?php $members = array ( "member1" => array( "name" => "John", "zodiac" => "Scorpio", "country" => "USA" ), "member2" => array( "name" => "Susan", "zodiac" => "Virgo", "country" => "Ireland" ), "member3" => array( "name" => "William", "zodiac" => "Pisces", "country" => "Great Britain" ), "member4" => array( "name" => "Eduardo", "zodiac" => "Leo", "country" => "Mexico" ) ); echo "Member Count: " . count($members); ?> This is an example of looping over multidimensional array data. $datasetCount = count($members); // returns count of array items of any array echo "<h1>There are $datasetCount members</h1>"; $i = 0; foreach ($members as $each_member) { $i++; echo "<h2>Member $i</h2>"; while (list($key, $value) = each ($each_member)) { echo "$key: $value<br />"; } }

Array Functions

PHP has over 50 built-in array functions for processing array data. These functions exist to help developers accomplish tasks that are common to array data handling in any programming language.

PHP

Fundamentals

IntroductionPHP Script TagsSyntax GuidelinesCheck PHP InformationData TypesVariablesConstantsUnderstanding Arrays