What is array in Php?

An array is a collection of variable. A simple variable can store a single value whether it is a string type, integer type or boolean type variable. In an array you can store multiple values.

There  are three type of arrays in Php:-
Numeric array – An array with a numeric index
Associative array – An array where each ID key is associated with a value
Multidimensional array – An array containing one or more arrays

1) Numeric Array -
$arr = array(“item-1″,”item-2″,”item-3″);

You can access the values of array like this:-
echo “first value= “.$arr[0];
echo “third value= “.$arr[2];

2) Associative array -
$arr = array(“key-1″=>”item-1″,”key-2″=>”item-2″,”key-3″=>”item-3″);

You can access the values of array like this:-
echo “first value= “.$arr["key-1"];
echo “third value= “.$arr["key-3"];

3) Multidimensional Arrays -
$arr = array( array(“key-1″=>”item-1″,”key-2″=>”item-2″), array(“key-1″=>”item-1″,”key-2″=>”item-2″));

You can access the values of array like this:-
echo “first row first column= “.$arr[0]["key-1"];
echo “second row second column= “.$arr[1]["key-2"];

Printing an Array

Many times you want to print all the values of an array may be some times for debugging a program. Here is the function print_r() which can be used to display array.


Counting the length of an array

If you want count the length of the php array than you can use the function count() to count the length of an array.

echo count($array);

Merging two or more array by using array merge php function

By using the array_merge() function you can merge two or more php array into one.


Implode in php

The implode() function convert an array into an string by combining all the elements with a separator.

eg: echo implode(” “,$arr);

Explode in php

The explode() function convert a string into an array dividing the array from a given separator.

eg: print_r( implode(” “,$string) );

2 Responses so far.

  1. diablo 3 says:

    Way awesome, some legitimate details! I enjoy you producing this short article offered, the remainder of the internet site is additionally substantial high quality. Possess a entertaining.

  2. Everything is really open and extremely clear explanation of concerns. was truly info. Your site is quite useful. Many thanks for sharing.