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″);
or
$arr[0]=”item-1″;
$arr[1]=”item-2″;
$arr[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″);
or
$arr["key-1"]=”item-1″;
$arr["key-2"]=”item-2″;
$arr["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″));
or
$arr[0]["key-1"]=”item-1″;
$arr[0]["key-2"]=”item-2″;
$arr[1]["key-1"]=”item-1″;
$arr[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.
print_r($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.
$new_array=array_merge($array1,$array2);
or
$new_array=array_merge($array1,$array2,$array3);
Implode in php
The implode() function convert an array into an string by combining all the elements with a separator.
implode(separator,array);
eg: echo implode(” “,$arr);
Explode in php
The explode() function convert a string into an array dividing the array from a given separator.
explode(separator,string);
eg: print_r( implode(” “,$string) );
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.
Everything is really open and extremely clear explanation of concerns. was truly info. Your site is quite useful. Many thanks for sharing.