PHP #3: Arrays 05-04-2013, 04:35 AM
#1
PHP #3: Arrays
By Hackarchives
Previous Tutorials:
PHP #1: Introduction
PHP #2: Basics
One of the main features of PHP is arrays.
An array in PHP is a collection of key/value pairs. This means that it maps values to keys. Values can be any type.
Each value stored in an array has a key.By default the keys start from 0.
In PHP there are two kind of arrays : indexed array and associative array. The only difference is that numeric values are used as 'keys' in indexed array start from zero (0) and in associative array strings are used as 'keys'
so in PHP,Arrays are of two types
1. One - Dimensional
2. Multi - Dimensional
ONE DIMENSIONAL ARRAYS
Example:
PHP Code:
$hc = array('hello','world','by','hack','archives');
In the above example we are storing strings in array.
'hello' is stored at key value 0, 'world' is stored at key value 1 and so on
Just use print_r to display information about the array
PHP Code:
$hc = array('hello','world','by','hack','archives');
print_r($hc);
OUTPUT:
Code:
Array ( [0] => hello [1] => world [2] => by [3] => hack [4] => archives )To access a value of an array you use the array variable with it's key.
PHP Code:
$hc = array('hello','world','by','hack','archives');
echo $hc[0].'<br />'.$hc[3].'<br />';
echo 'This program is created by '.$hc[3].' '.$hc[4];
OUTPUT:
Code:
hello
hack
This program is created by hack archivesA great feature of arrays in PHP is that you can create your own keys.
The syntax is
PHP Code:
array(key1 => value1,
key2 => value2,
key3 => value3
....
);
Example
PHP Code:
$hc = array(
"blah" => "hello",
"bluh" => "world",
"oh" => "by",
"stop" => "hack",
"now" => "archives"
);
Using print_r function on $hc would produce the following result
Code:
Array ( [blah] => hello [bluh] => world [oh] => by [stop] => hack [now] => archives )These can be accessed by just calling them with their keys.
Note: Array keys (or indexes) may be either an integers or a strings.
You can store information or modify the information easily using
Code:
Syntax:
arrayvariable[key] = value;Example
PHP Code:
$hc[0] = "bye";
echo $hc[0];
Code:
byeMULTI DIMENSIONAL ARRAYS
Multi dimensional arrays are simply arrays of arrays
A multidimensional array is an array containing one or more arrays.
In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.
PHP Code:
$hc = array(
"admins"=>array('bluedog','1llusion'),
"fruits"=>array(
"fresh"=>array('apple','mango'),
"stale"=>array('grapes','bananas'),
)
);
ON using print_r on the above variable $hc would produce the following result
Code:
Array ( [admins] => Array ( [0] => bluedog [1] => 1llusion ) [fruits] => Array ( [fresh] => Array ( [0] => apple [1] => mango ) [stale] => Array ( [0] => grapes [1] => bananas ) ) )Now you can call the values the same way just add a key of subarray after key of parent array.
For example you wish to get bluedog on screen, so your call will be
PHP Code:
<?php
$hc = array(
"admins"=>array('bluedog','1llusion'),
"fruits"=>array(
"fresh"=>array('apple','mango'),
"stale"=>array('grapes','bananas'),
)
);
echo $hc[admins][0];
?>OUTPUT:
Code:
bluedogPHP Code:
<?php
$hc = array(
"admins"=>array('bluedog','1llusion'),
"fruits"=>array(
"fresh"=>array('apple','mango'),
"stale"=>array('grapes','bananas'),
)
);
echo $hc[fruits][stale][1];
?>OUTPUT:
Code:
bananasNOTE: As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].
That's all for now.Hope you understood it.Feel free to ask any queries in replies




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: LGqjaYA.gif]](http://i.imgur.com/LGqjaYA.gif)