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>";
//print_r($arr2);

/*foreach loop*/  
$arr3=array('a'=>'America','b'=>'Bangladesh','c'=>'China','d'=>4);
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:

$arr4=[4,5,6,'a'=>'America','b'=>'Bangladesh'];
//print_r($arr4);

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.
Example:

$arr6=[2,4,5=>6,9,8,3=>'a','b'];
//echo $arr6[8];
	
$multi=[4,5,3=>[1,2,9],['a','b','c'=>['x','y','z']]];
//echo $multi[4]['c'][2];

$multip=[[[[[[[10],9]]]]],11,12,13=>[15]];
echo $multip[0][0][0][0][0][1];
print_r($multip);

Array Function:

// extract function used with array
	$arr8=['u'=>'University','i'=>'index','m'=>'model'];
	extract($arr8); // if you want to convert associative array index as variable
	//echo $m;
	
// EXTR_PREFIX_ALL function used with array
	$arr9=[5,8,9,10];
	extract($arr9,EXTR_PREFIX_ALL,'b');
	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.
	

Stretching Horizontal Navigation Menus

[ this selector could be div#nav instead of nav depending on which tag you wrapped the ul in ]
html:

<nav>
	<ul>
		<li><a href="">Home Page</a></li>
		<li><a href="">About Us</a></li>
		<li><a href="">Contact Us</a></li>
	</ul>
</nav>

css:

nav{display: table;width: 100%;border-collapse: collapse;border: none;}
nav ul {display: table-row;}
nav li {display: table-cell;margin: 0;}