PHP array


An array can store multiple values ​​in a single variable:

Instance

 <?php
 $cars=array("Volvo","BMW","Toyota"); 
 echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[ 2] . "."; 
 ?>

What is an array?


An array is a special variable that can store multiple values ​​in a single variable.

If you have a list of items (for example: a list of car names), store them in a single variable as follows:

$cars1="Volvo"; 
$cars2="BMW"; 
$cars3="Toyota";

However, what if you want to iterate through the array and find a specific one? If the array has more than 3 items but 300?

The solution is to create an array!

Arrays can store multiple values ​​in a single variable, and you can access the values ​​based on them.

Create an array in PHP


In PHP, the array() function is used to create an array:

Array();

In PHP, there are three types of arrays:

· Numeric array - an array with a numeric ID key

· Associative array - an array with the specified key, each key associated with a value

· Multidimensional arrays - arrays containing one or more arrays

PHP numeric array


There are two ways to create a numeric array:

The ID key is automatically assigned (the ID key always starts at 0):

$cars=array("Volvo","BMW","Toyota");

Manually assign an ID key:

$cars[0]="Volvo"; 
$cars[1]="BMW"; 
$cars[2]="Toyota";

The following example creates an array of values ​​called $cars and assigns three elements to the array, then prints a piece of text containing the array values:

Instance

<?php
 $cars=array("Volvo","BMW","Toyota"); 
 echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[ 2] . ".";  
 ?>
Get the length of the array - count() function

The count() function returns the length of the array (the number of elements):

Instance

 $cars=array("Volvo","BMW","Toyota"); 
 echo count($cars); 
 ?>

Traversing an array of values


To iterate over and print all the values ​​in the value array, you can use a for loop as follows:

Instance

 <?php
 $cars=array("Volvo","BMW","Toyota"); 
 $arrlength=count($cars); 
 
 for($x=0;$x<$arrlength;$x++) 
 { 
 echo $cars [$x]; 
 echo "
"; 
 } 
 ?>

PHP associative array


An associative array is an array that uses the specified key that you assign to the array.

There are two ways to create an associative array:

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
Or:

$age['Peter']="35"; 
$age['Ben']="37"; 
$age['Joe']="43";

The specified key can then be used in the script:

Instance
 <?php
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); 
 echo "Peter is " . $age['Peter'] . " years old."; 
 ?>

Traversing associative arrays


By traversing and printing all the values ​​in the associative array, you can use a foreach loop as follows:

Instance

 <?php
 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); 
 
 foreach($age as $x=>$x_value) 
 { 
 echo "Key=" . $x . ", Value=" . $x_value; 
 echo "
"; 
 } 
 ?>

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :