5/7/12

A Tutorial on Arrays in PHP

Arrays in all languages whether Java, C++, Python, C etc are the same. 


Definition, Arrays are complex variables that store a group of values under a single variable name.



Creating Arrays


Its difficult for a person who is new to PHP to create an array, because arrays every language is created differently though they do the the same thing. This was a problem which I also had initially when I started coding in php.




So, the ways in which an array can be created in php are :

* Using the key/value pair :


$arrayname['key1'] = value1;
$arrayname['key2'] = value2;
$arrayname['key3'] = value3;

NOTE : 

* THE KEY IS REFERRED TO AS INDEX.
** The key in the array can be either a number or a string.

** Without key/value pair :


$arrayname[] = value1;
$arrayname[] = value2;
$arrayname[] = value3;

NOTE : 

The First value in an array always has the index as 0 unless until you have specified some different number.


*** Using the construct array() :



$arrayname = array( "Code", "2","Learn");
The above statement creates an array with 3 values and the key starting value being 0. 

**** Using the construct array() using the key/value pair :



$arrayname = array( "key1" => value1, "key2" => value2, "key3" => value3 );

The above statement will create an array with specific key/value pair. The key/index can be set either a number or string.

***** Using the range(start,end) function :



$arrayname = range(100,110);

The above statement creates an array with a range of values specified.

Viewing Arrays



There are two inbuilt functions that PHP provides us, they are var_dump() and print_r(). Both function helps us to see the structure and values of any array.

Ex. 

print_r($arrayname);

The statement give the following output :

Array
(
    [key1] => value1
    [key2] => value2
    [key3] => value3
)

The key difference between var_dump($arrayname) & print_r($arrayname) is that, var_dump($arrayname) along with key/value pair gives the data type of the value stored.

Ex. :
var_dump($arrayname);
OUTPUT :

Array
(
    [key1] => 
    data_type(size) value1
    [key2] => 
    data_type(size) value2
    [key3] => 
    data_type(size) value3
)


Deleting from Array


Deleting from array in php is done with respect to the index/key. There is an inbuilt function in php known as unset($arrayname[key/index]).

The unset() function allows removing keys from an array. Be aware that the array will not be reindexed. 

Now to reindex the values we need to use the array_values() function to reindex the array.

The array_values($arrayname) function takes the array as the input and then reindexes it.

Example :



<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
print_r($a);

$b = array_values($a);
echo "after array_values";
print_r($b);
?>

OUTPUT :

Array (

1 => 'one',
3 => 'three'
)

after array_values

Array (

1 => 'one',
2 => 'three'
)

Keep yourself up to date with the posts at Code 2 Learn, by subscribing to it. Click Here to subscribe (check your email spam too).

SHARE THIS POST:

0 comments:

Post a Comment