The page describes the basic concepts of creating, managing, accessing PHP arrays, most populat functions and operators used for this purpose.
There are several basic variable types in PHP. Array is one of them. As everybody know due to its dynamic nature PHP does not require type definitions, and variables automatically appear when you mention them for the first time, and can change their type during execution.
Internally, PHP array, unlike array variables in classic static languages like C, is not a sequence of elements of one predefined type. PHP array is a hash table representing a reflection of keys to their values, where values can belong to any type. That is why you can easily modify PHP arrays, insert and remove their elements without need to repack the arrays' internal memory chunks. However, access time for PHP array remains O(1).
Despite the fact that PHP array is a hash, it has its beginning and its end. The keys which were defined first are closer to the beginning.
To point a PHP variable as array you should use the array() construction function.
<?php ?>
The example above creates $myArray variable as an empty PHP array.
In general the construction looks like:
<?php [key1] => value1, [key2] => value2, ... [keyN] => valueN ); ?>
For example:
Key values in PHP array() function are optional. You can omit them. In this case PHP automatically assigns integer values starting with 0 to the array's keys. The following calls are equal:
If you skip some of the keys PHP will set them using the previously defined keys. The following calls are equal:
You can always check whether a PHP variable is an array or not with help of is_array() function. It returns boolean true/false values which are usually used in conditions.
PHP uses the same syntax most popular languages use to access array values.
<?php "name" => "John", "surname" => "Smith", "age" => 33, "sex" => "m" ); $PHPArray[ "children"] = array( "Mary", "Brandon", "Lora" ); // Now the $PHPArray variable is equal to the $userInfo variable mentioned earlier. $surname = $PHPArray[ "surname" ]; // $surname contains "Smith" $lora = $PHPArray[ "children"][ 2 ]; // $lora contains "Lora" ?>
There are several ways to add new values to array. The main is setting new value by key as shown in the example above where we added "children" key pointing to a newly created array of children's names.
The second way is operator []= which appends a new element to the end of array:
<?php $PHPArray []= "Orange"; // Now $PHPArray contains array( 0 => "Banana", 5 => "Apple", 6 => "Orange" ); ?>
The third way is using functions like array_merge() and array_merge_recursive(), and + operator.
To find out how many elements there are in an array use count() function. It returns the amount of the first-level's elements.
If the array's keys are integers without gaps then the simplest way to go through it is for operator.
There is more advanced and more powerful foreach operator which covers all kinds of standard PHP arrays.
<?php "name" => "John", "surname" => "Smith", "age" => 33, "sex" => "m" ); foreach ( $userInfo as $elementValue ); // $elementValue changes from "John" to "m" // $elementValue contains "m" ?>
If you wish to get keys of your PHP array inside foreach together with the values use the following foreach syntax:
<?php "name" => "John", "surname" => "Smith", "age" => 33, "sex" => "m" ); foreach ( $userInfo as $elementKey => $elementValue ); // $ elementKey changes from "name" to "sex", the behavior of $elementValue remains the same as in the previous example // $elementKey contains "sex", $elementValue contains "m" ?>
Both versions of foreach shown above iterate arrays getting $elementValue by value, which means that changing $elementValue inside foreach does not affect $userInfo itself. To get direct access to PHP array's values use the following syntax (mention the & sign):
<?php "name" => "John", "surname" => "Smith", "age" => 33, "sex" => "m", ); // modify the $PHPArray so that all keys will point to the strings held in the keys themselves ( "name" => "name", "surname" => "surname" : ) foreach ($PHPArray as $elementKey => &$elementValue ) $elementValue = $elementKey; ?>
Another way to iterate PHP arrays is the combination of reset(), list() and each() functions inside for operator. Here is how it looks like:
Our story so far: Chapter 1: Apple creates the iPhone. Chapter 2: Apple opens the App Store, an online catalog of cheap or free programs that you can download straight to the phone. Programmers all over the world write 70,000 apps for it that... read more...
Apple released a Mac OS X Leopard upgrade that brings better stability and security to the operating system and improves compatibility with AirPort wireless networks and other Apple technology. Mac OS X 10.5.8 was made available from Apple on... read more...
Telco Planning has joined The Linux Foundation, the nonprofit organization dedicated to accelerating the growth of Linux. Telco Planning provides consulting services to network operators that encompass technology evaluation, business plan modeling,... read more...
Thursday's denial-of-service attack that knocked Twitter offline for a few hours and affected Facebook, LiveJournal, and Google Sites and Blogger wasn't your average attack. Typically, someone who has a bone to pick with a specific Web site will... read more...
There are several basic variable types in PHP. Array is one of them. As everybody know due to its dynamic nature PHP does not require type definitions, and variables automatically appear when you mention them for the first time, and can change their... read more...
Symantec Corporation was founded in 1982 by Gary Hendrix with a National Science Foundation grant. Symantec was originally focused on artificial intelligence-related projects, including a database program. Hendrix hired several Stanford University... read more...
International Business Machines Corporation, abbreviated IBM and nicknamed "Big Blue" (for its official corporate color), is a multinational computer technology and IT consulting corporation headquartered in Armonk, New York, United... read more...
Apple Inc. is an American multinational corporation that designs and manufactures consumer electronics and computer software products. The company's best-known hardware products include Macintosh computers, the iPod and the iPhone. Apple software... read more...
Oracle Corporation specializes in developing and marketing enterprise software products - particularly database management systems. Through organic growth and a number of high-profile acquisitions, Oracle enlarged its share of the software market.... read more...
Bookmark this page