PHP Arrays !!
In this article we will discuss about PHP Arrays, Types of PHP arrays in brief.
PHP Array
An array is a special variable that stores multiple values at a time.
Array can store values of int, string, float, character etc..
Types Of Array
There are three types of array
- Numeric
- Associative
- Multidimensional
Numeric Array
In numeric array each element having numeric key associated with it that is starting from 0.
You can use array() function to create array.
Syntax :
$array_name=array ( value1, value2 …valueN)
Program :
<?php
$my=array(‘A’,’B’,’C’);
print_r($my);
?>
Output:
Array( [0]=>A [1]=>B [2]=>C)
You can refer to individual element of an array in PHP script using its key values.
Progarm :
<?php
$myarray=array(‘A’,’B’,’C’);
echo $my[1];
?>
Output : B
In Numeric Array you can use for, while or do while loop to iterate through each element in array
because in numeric array key values are consecutive.
Program :
<?php
$myarray=array(“Apache”, “MySQL”, “PHP”);
for($i=0;$i<3;$i++)
{
echo $myarray*$i+.”<br>”;
}
?>
Output :
Apache
MySQL
PHP
Associative Array
The associative part means that arrays store element values in association with key values.
If you store an element in an array, in association with a key, all you need to retrieve it later from
that array is the key value.
Key may be either numeric or string.
You can use array() function to create associative array.
Syntax :
$array_name=array(key1=>value1, key1=>value1,….. keyN=>valueN);
Program :
<?php
$myarray=array(5=>”Apple”, 10=>”Mango”, 20=>”Grapes”);
print_r($my_array);
?>
Output:
Array([5]=>Apple [10]=>Mango [20]=>Grapes)
In associative array you cannot use for, while, do..while loop to iterate through each element in
array because in Associative array key value are not consecutive.
So you have to use foreach loop.
Program :
<?php
$myarray=array(“Name”=>”James” , “Age”=>25, “Gender”=>”Male” );
foreach($myarray as $item)
{
echo $item;
}
?>
Output :
James 25 Male
Multidimensional Array
PHP can easily support multidimensional arrays, with arbitrary numbers of key.And just as in one
dimensional arrays, there is no need to declare out intentions in advance. e:
The integer key of 0 stores a string, and the key 1 stores an array that ,in turn, has a string in it.
Creating array within another array using array() construct is as follows:
$basket=array( ‘fruit’ => array(‘red’ => ‘apple’,
‘yellow’ => ‘banana’,
‘black’ => ‘grapes’),
‘flower’ =>array(‘red’ => ‘rose’,
‘yellow’ => ‘sunflower’,
‘purple’ => ‘iris’)
);
It is simply an array with two values stored in association with keys.
Each of them values is an array itself. We can reference it like this
echo $basket*‘fruit’+*‘black’+;
Output : grapes
$kind=”flower”;
$color=”yellow”;
print(“ $basket *$kind+ *$color+”);
Output : sunflower
In this article we will discuss about Numeric, Associative and Multidimensional array of PHP in brief.
Mesothelioma Survival Rates , Mesothelioma Attorneys California , Seattle Mesothelioma Lawyer , Mesothelioma Suit , Mesothelioma Claim , Wisconsin Mesothelioma Attorney , Mesothelioma Attorney illinois , Mesothelioma Trial Attorney ,Houston Mesothelioma Attorney , Mesothelioma Lawyer Dallas , New York Mesothelioma Law Firm,Mesothelioma Lawyer Asbestos Cancer Lawsuit , Mesothelioma Lawyers San Diego,Mesothelioma Litigation , Mesothelioma Attorney Assistance , Mesothelioma Lawyer Asbestos Cancer Lawsuit , Mesothelioma Lawyer Chicago , New York Mesothelioma Law Firm ,
Houston Mesothelioma Attorney , Mesothelioma Lawyer Dallas , Mesothelioma Care , Mesothelioma Trial Attorney , Mesothelioma Lawyer Texas , Lawsuit Mesothelioma , Truck Accident Attorney Los Angeles ,Asbestos Exposure Lawyers , Mesothelioma Cases , Emergency Response Plan ppt , Mesothelioma Lawyers San Diego , Asbestos Mesothelioma Lawsuit , Tennessee Mesothelioma Lawyer , Meso Lawyer , Mesothelioma Settlements Amounts , Mesothelioma Law Firms , New Mexico Mesothelioma Lawyer , Accident Attorneys Orange County , Mesothelioma Lawsuit ,Personal Injury Accident Lawyer , Purchase Structured Settlements , Firm Law Mesothelioma , Car Accident Lawyers Los Angeles , Mesothelioma Attorneys , Structured Settlement Company , Auto Accident Lawyer San Francisco , Mesotheolima , Los Angeles Motorcycle Accident Lawyer
Mesothelioma , Mesothelioma Lawyer Virginia , Seattle Mesothelioma Lawyer , Mesothelioma Attorney illinois , Mesothelioma Drug , Mesothelioma Attorney DirectoryMesothelioma Settlements , Mesothelioma Help , Mesothelioma ct , Mesothelioma Attorney Florida , Washington Mesothelioma Attorney , Best Mesothelioma Lawyers , Diagnosed With Mesothelioma , Mesothelioma Settlement Amounts , Mesothelioma Personal Injury Lawyers , Mesothelioma Compensation , Attorney Lawyer Mesothelioma ,Colorado Mesothelioma Lawyers,Mesothelioma Lawsuits , Mesothelioma Claims , Average Mesothelioma Settlement , Average Mesothelioma Settlement , Mesothelioma Law Suit , Alabama Mesothelioma Lawyer , alabama Mesothelioma Lawyer , utah mesothelioma lawyer , Dallas Mesothelioma Lawyer , Mesothelioma Attorney California , Mesothelioma Lawyer California ,Mesothelioma Survival Rates ,
Mesothelioma Attorneys California, Wisconsin Mesothelioma Attorney Mesotheliama , Mesothelioma Lawyer Virginia , Seattle Mesothelioma Lawyer , Mesothelioma Attorney illinois, Mesothelioma Trial Attorney Injury Lawyer Houston tx , Baltimore Mesothelioma Attorneys , Mesothelioma Care ,Mesothelioma Trial Attorney ,Mesothelioma Lawyer Texas , Mesothelioma Lawyer Dallas , Mesothelioma Care , Mesothelioma Trial Attorney , Mesothelioma Lawyer Texas , California Mesothelioma Attorney Philadelphia Mesothelioma Lawyer , Illinois Mesothelioma Lawyer , Mesothelioma Attorney , Mesothelioma Claims , Average Mesothelioma Settlement
Integrated ehr , Mesothelioma Law Sui , Mesothelioma Lawyer California,Mesothelioma Attorney California ,Mesothelioma Law Suits ,Dallas Mesothelioma Attorneys ,Mesothelioma Drug ,Mesothelioma Attorney Directory