saftutorials.com
PHP Arrays Tutorial

An array can store one or more similar types of values in a single value. If you want to store multiple fruits name you can store it in a single array and each array value accessed by its id which is called index.


There are three different types of array
Numeric Array - Its index will be numeric
Associative Array - Its index will be string
Multidimensional Array - This array contains multiple arrays

Example of Numeric Arrays

The PHP var_dump function return also datatypes

<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('apple', 'banana', 'mango');
var_dump($fruits);
echo $fruits[0].'</br>';
echo $fruits[1].'</br>';
echo $fruits[2];
?>
</body>
</html>


The above code produces the following results

Outputs: array(3) { [0]=> string(5) 'apple' [1]=> string(6) 'banana' [2]=> string(5) 'mango' }

apple

banana

mango


Example of Associative Arrays

The PHP var_dump function return also datatypes

<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array('first_fruit' => 'apple', 'second_fruit' => 'banana', 'third_fruit' =>  'mango');
var_dump($fruits);
echo $fruits['first_fruit'].'<br />';
echo $fruits['second_fruit'].'<br />';
echo $fruits['third_fruit'];
?>
</body>
</html>


The above code produces the following results

Outputs: array(3) { ['first_fruit']=> string(5) 'apple' ['second_fruit']=> string(6) 'banana' ['third_fruit']=> string(5) 'mango' }

apple

banana

mango

Note: Do not keep the associative array in double quotes while printing otherwise it will not return any value.



Example of Multidimensional Arrays

The PHP var_dump function return also datatypes

<!DOCTYPE html>
<html>
<body>
<?php
$employee_details = array( 
"Jakob" => array (
 "mobile" => '19889234561',
 "email" => 'jakob@gmail.com',
 "salary" => 20000
),
"John" => array (
 "mobile" => '18786756666',
 "email" => 'john@gmail.com',
 "salary" => 30000
),
"AdamP" => array (
 "mobile" => '7896756432',
 "email" => 'adamp@gmail.com',
 "salary" => 35000
)
);
/* Accessing multi-dimensional array values */
echo "Jakob mobile no - ".$employee_details['Jakob']['mobile'] . "<br />"; 
echo "Jakob email id - ".$employee_details['Jakob']['email'] . "<br />"; 
echo "Jakob salary - ".$employee_details['Jakob']['salary'] . "<br /><br />";
echo "John mobile no - ".$employee_details['John']['mobile'] . "<br />"; 
echo "John email id - ".$employee_details['John']['email'] . "<br />"; 
echo "John salary - ".$employee_details['John']['salary'] . "<br /><br />"; 
echo "AdamP mobile no - ".$employee_details['AdamP']['mobile'] . "<br />"; 
echo "AdamP email id - ".$employee_details['AdamP']['email'] . "<br />"; 
echo "AdamP salary - ".$employee_details['AdamP']['salary']; 
?>
</body>
</html>


The above code produces the following results

Outputs: Jakob mobile no - 19889234561

Jakob email id - rakesh@gmail.com

Jakob salary - 20000


John mobile no - 17896756432

John email id - john@gmail.com

John salary - 30000


Adamp mobile no - 17896756432

Adamp email id - adamp@gmail.com

Adamp salary - 35000


Note: Do not keep the associative array in double quotes while printing otherwise it will not return any value.


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic