Login Register






PHP #3: Arrays filter_list
Author
Message
PHP #3: Arrays #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 )
Here you can clearly see the keys of the array.

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 archives

A 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];
In this case output will be

Code:
bye

MULTI 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:
bluedog
Now suppose you want to print second fruit which is stale.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[fruits][stale][1]; ?>

OUTPUT:
Code:
bananas

NOTE: 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 Smile

Reply

PHP #3: Arrays #2
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 )
Here you can clearly see the keys of the array.

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 archives

A 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];
In this case output will be

Code:
bye

MULTI 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:
bluedog
Now suppose you want to print second fruit which is stale.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[fruits][stale][1]; ?>

OUTPUT:
Code:
bananas

NOTE: 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 Smile

Reply

RE: PHP #3: Arrays #3
Wow great tutorial mate =D :thumbs:
Know PHP - HTML 5 - CSS3

Learning C

Hello there, [username] Need Help PM me now!


Please Visit my friends and I website : Creative Programming


[Image: LGqjaYA.gif]

Reply

RE: PHP #3: Arrays #4
Wow great tutorial mate =D :thumbs:
Know PHP - HTML 5 - CSS3

Learning C

Hello there, [username] Need Help PM me now!


Please Visit my friends and I website : Creative Programming


[Image: LGqjaYA.gif]

Reply

RE: PHP #3: Arrays #5
Great and clear tutorial about arrays : )

Reply

RE: PHP #3: Arrays #6
Great and clear tutorial about arrays : )

Reply

RE: PHP #3: Arrays #7
(05-05-2013, 01:40 PM)TheDarkNight Wrote: Wow great tutorial mate =D :thumbs:

(05-05-2013, 01:41 PM)bluedog.tar.gz Wrote: Great and clear tutorial about arrays : )

You're welcome guys..I will be posting 4th part soon Smile

Reply

RE: PHP #3: Arrays #8
(05-05-2013, 01:40 PM)TheDarkNight Wrote: Wow great tutorial mate =D :thumbs:

(05-05-2013, 01:41 PM)bluedog.tar.gz Wrote: Great and clear tutorial about arrays : )

You're welcome guys..I will be posting 4th part soon Smile

Reply