Arrays are variables used to store multiple values in a single variable name, each value being a string or anumber or even another array.
Arrays are structured as a series of key-value pairs, where one pair is an element of that array. For each item in the list, there is a key (or index) associated with it.
In PHP there are two kind of Arrays: Numeric Arrays (known as indexed arrays) and Associative Arrays.
$array[0] = value0; $array[1] = value1; $array[2] = value2; ...The key (within square bracket) can start at any number.
$array[] = value0; $array[] = value1; $array[] = value2; ...In this way, PHP automatically assigns the keys, starting at 0 (value0 will have associated the index 0).
<?php // specifying the keys for each element $array[0] = 'php'; $array[1] = 'mysql'; $array[2] = 8; // without specifying the keys $fruits[] = 'apple'; $fruits[] = 'banana'; $fruits[] = 'apricots'; ?>
$array = array(0=>value0, 1=>value1, 2=>value2);The first key can start at any number. You can add as many key=>value pairs as you want (separated by commas).
$array = array(value0, value1, value2);In this way, PHP automatically assigns the keys, starting at 0 (value0 will have associated the index 0).
<?php // specifying the keys for each element $array = array(0=>'php', 1=>'mysql', 2=>8); // without specifying the keys $fruits = array('apple', 'banana', 'apricots'); ?>
The keys of any array have to be named uniquely, otherwise each identical key assigned will replace the one before it.
If you want to create an empty array, just use the array function without any parameters:
$arr = array();
$var_name = $array[key];The value of the array element with the specified key will be stored in the $var_name variable.
<?php $array = array('php', 'mysql', 8); $elm = $array[1]; echo $elm. '<br />'; // mysql echo $array[0]. ' course'; // php course ?>In this example the keys aren't added when the array is defined, but we know that PHP will assign automatically the keys, starting at 0.
To get the number of elements in an array you can use the count() function:
$nr = count($array);
array_name[index] = 'new value';If the array_name[index] element isn't defined in that array, will be added automatically with the 'new value'. This method can be used when you want to add new elements in an array.
<?php // define an array $fruits = array('apple', 'banana', 'apricots'); // change the value of first element $fruits[0] = 'figs'; $nr = count($fruits); // gets the number of items in $fruits echo 'There is '. $nr. ' elements in the array. The first is: '. $fruits[0]; // adds a new item in $fruits $fruits[] = 'nuts'; $nr = count($fruits); // gets again the number of items in $fruits echo '<br /> Now, the $fruits array has '. $nr. ' items'; ?>To add a new element in a numeric array, you can also use the syntax: $array[] = value; (for example: $fruits[] = 'nuts';).
$nr = count($array); // gets the number of elements for($i=0; $i<$nr; $i++) { // PHP instructions for the curent element: $array[$i]; }- Example:
<?php $arr = array('php', 'mysql', 8); $nr = count($arr); for($i=0; $i<$nr; $i++) { $elm = $arr[$i]; echo '<br /> Item '. $i. ' has the value: '. $elm; } ?>Output;
$array['key1'] = value1; $array['key2'] = value2; $array['key3'] = value3; ...You can use either single or double quotes.
<?php $sites['en'] = 'www.courseweb.net'; $sites['ro'] = 'marplo.net'; $sites['search'] = 'www.google.com'; ?>
$sites = array('key1'=>value1, 'key2'=>value2, 'key3'=>'value3');You can add as many 'key'=>value pairs as you want (separated by commas).
<?php $sites = array('en'=>'www.courseweb.net', 'ro'=>'marplo.net', 'search'=>'www.google.com'); ?>
<?php // define the array $sites['ro'] = 'www.courseweb.net'; $sites['en'] = 'marplo.net'; $sites['search'] = 'www.google.com'; echo 'Search: '. $sites['search']; // Search: www.google.com // change the value of 'search' item $sites['search'] = 'www.yahoo.com'; echo '<br /> Search: '. $sites['search']; // Search: www.yahoo.com ?>This code will output:
foreach ($array as $key => $val) { // Do something with $key and /or $val. }This construct will place the key and value of the current $array element into their own variables: $key and $val.
<?php // define the array $sites['ro'] = 'www.courseweb.net'; $sites['en'] = 'marplo.net'; $sites['search'] = 'www.google.com'; // traversing $sites foreach ($sites as $key => $val) { echo '<br /> The key is: '. $key. ' and its value is: '. $val; } ?>This code will output:
$arr = []; // empty array, instead of array(); $arr = [1, 2, 3, 4]; // numeric array $arr = ['ro'=>'courseweb.net', 'en'=>'marplo.net', 'num'=>8]; // associative array- The array created with this short syntax can be accessed, modified and traversed as any array.
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250"> <param name="src" value="file.swf" /> Your browser not support SWF. </object>
input:focus { background-color: #88fe88; }
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}'; var obj = JSON.parse(jsnstr); alert(obj.url);
if (file_put_contents("file.txt", "content")) echo "The file was created"; else echo "The file can not be created";