Surej P John

All about SUREJ

Creating Arrays in PhP

without comments

php_5.3

 

PHP is regarded as the most popular server side scripting language. Since its origin in 1995, PHP is growing every day due to its characteristics such as its very simple to learn and use, very fast, its free, its a portable XHTML-embedded scripting language, well suited for designing and managing database enabled web-pages.  I am not going to talk everything about PHP (though I wish to) today in this post. I love to share some of my thoughts about various functions used in PHP for creating, modifying, checking, sorting arrays. My intention is to help those learning this wonderful language as much as I can by giving some explanations and simple examples.

Function used for Creating Arrays:

According to the PHP Manual, an array is an ordered map where a map is a type that maps values to keys. In simple words, an array is combination of key-value pairs. The key or an index of an array can be a number or a string or both which is used to identify the corresponding value in the key. The value of an array is known as element which can be of any datatypes. There are two types of arrays; a numeric array which is indexed by a number and an associative array which is indexed by a string.


 Following are the functions used to create an array:

 

array What does this mean?
array() Creates an array
array_combine() Creates a new array[3] after combining 2 arrays. The new array will have the keys of array[1] and values of array[2]
array_fill() Fills an array with values.
array_pad() Pads an array to the specific length set by user with a value
range() Creates an array containing a range of values

Apart from this, another method is just use array identifier[] and PHP automatically creates a numeric array with index starting 0.

Here are some examples for the above functions.

$names=array (‘John’, ‘Surej’);

This is a numeric array. Here $names[0]=John and $names[1]=Surej


$my_colors=array  (2=>’red’, 3=> ‘green’);

This is a numeric array and key specified. Here $my_colors[2]=red and $my_colors[3]=green.


$Job=array (‘Dept’=>’Marketing’, ‘position’=> ‘Lecturer’);

This is an associative array. here $job[Dept]=Marketing and $Job[position]=Lecturer


echo $nums= range(1,10);

The above array will output 1 2 3 4 5 6 7 8 9 10 since the function asked to show numbers from 1 to max.10


echo $numb= range(1,10,2);

This function will output 1 3 5 7 9 since the function asked to show numbers from 1 to 10 with an increment of 2.


$love =array_fill(1,5,”I LOVE YOU”);

Array_fill function fills the array named love with index starting from 1 and values for all five arrays as I LOVE YOU.

ie $love[1]=$love[2]=$love[3]=$love[4]=$love[5]=I LOVE YOU.


If you need to see the elements of an array, you can use print_r() and var_dump() functions.

Print_r() Displays all the elements(values) of an array
var_dump() Displays the array, the number of elements in it, and the length of each string values.

There are many other functions which are used in PhP for various applications related to arrays. For example, to check whether the given key or index exists in an array, we use array_key_exists() or to check if the value exists in array, we use in_array() function.  I will post some of those functions and its explanations in the next post. Thanks for reading…

Written by Surej P John

June 13th, 2014 at 9:36 am

Posted in General

Tagged with , ,

Leave a Reply