16. Array

Array: An array is a special type of variable that can hold many values at once all accessible via a single variable name.Arrays are very useful whenever you need to work with large amounts of data — such as records from a database — or group related data together.

How arrays work:

  • An array can hold any number of values.
  • Each value in an array is called an element.
  • You access each element via its index, which is a numeric or string value. Every element in an array has its own unique index.
  • An element can store any type of value, such as an integer, a string, or a Boolean.

There are three different kind of arrays:

  1. Numeric array − An array with a numeric index
  2. Associative array − An array with strings as index
  3. Multidimensional array or Nested Array − An array containing one or more arrays

Indexed Array or Numeric Array:
These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

Numeric array example:

 $arr=[3,5,7,9.5,true,"a","c","eeee"];        //latest Method
//$arr=array(3,5,7,9.7,true,"a","c","e");    //old Method
echo "<pre>";        //html tag
print_r($arr);      //function
//var_dump($arr);  // funcion
//echo $arr[6];   // If you want to access single element

for($i=0;$i<count($arr); $i++){  // If you want to access all element
//echo $arr[$i],"<br>";

Associative Array:
The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string.
To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

associative array example:

$arr2=['a'=>'America','b'=>'Bangladesh','c'=>'China'];                   //latest Method
//$arr2=array('a'=>'America','b'=>'Bangladesh','c'=>'China','d'=>4);    //old Method
echo "<pre>";

/*foreach loop*/  
echo "<pre>";
foreach($arr3 as $a){           // If you want to only value, for this
	//echo $a,"<br>"; 

foreach($arr3 as $k=>$a){      // If you want to access index, for this 
	//echo $k,"<br>";

foreach($arr3 as $k=>$a){     // index and value 
	//echo $k,"=>", $a,"<br>";

Hybrid array Example:


Multidimensional Arrays or nested Arrays:
A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

//echo $arr6[8];
//echo $multi[4]['c'][2];

echo $multip[0][0][0][0][0][1];

Array Function:

// extract function used with array
	extract($arr8); // if you want to convert associative array index as variable
	//echo $m;
// EXTR_PREFIX_ALL function used with array
	echo $b_2;
// Normally numerical array's index does not converted in variable, because these index is number.but if you want to convert numeric index as variable, You can use function.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s