Arrays in PHP

Spread the love

Arrays in PHP

Definition: Arrays in PHP are the special type of variable which can store similar type of data/ values inside a single variable. An array is one of the basic data structures.
We can access each array variable using a key known as an index of the array. An array index can be numeric or string. Follow below description for more.

Example: If we want to store 10 employees names then we don’t need to define 10 variables, we can store those names inside array variable having the length of 10;

 

Defining Arrays in PHP:

<?php

$names = array();
$first_names[] = 'Vijay';
$ages = array(22, 26, 24 , 43, 23);

?>

Types of arrays:

There are three types of arrays present in PHP.

  1. Numeric arrays
  2. Associative array
  3. Multidimensional arrays

 

Numeric arrays:

In this type of arrays, index keys are numeric. Its index values start from zero and values are stored and accessed using linear fashion. We can store any type of data in these arrays.

Example:

Defining numeric arrays:

1. $names = array(“andy”,”sam”,”trisha”,”sandy”);// Assigning values at the time od array declaration

echo “Name at index 0 = “.$names[0];

Output: Name at index 0 = andy

//Assigning single values

2. $names[0] = “andy”;
$names[1] = “sam”;
$names[2] = “trisha”;
$names[3] = “sandy”;

Print all the array values:

We can print use foreach loop to print all the array values. foreach loop is useful when we are not sure about the number of array values present in array. Let see an example.

<?php
$i =0;
foreach($names as $name)
{
echo "Name at index ".$i." = ".$name."<br>";
$i++
}
?>

Output:

Name at index 0 = andy
Name at index 1 = sam
Name at index 2 = trish
Name at index 3 = sandy

We can print values for debugging purpose using print_r, as below.
print_r($names);

Array
(
[O] => andy
[1] => sam
[2] => trish
[3] => sandy
)

Associative arrays:

Associative arrays are having strings as their index. As opposite to numeric arrays where indexes are always numbers, associative arrays can have a string as their index.
We can store values in associative arrays in key – value fashion. More on this see below examples.

Example:

Declaring associative array with key value pairs

$emps = array(“andy” => 1000, “sam” => 1002, “trisha” => 1004, “sandy” => 1006);

echo “Employee number of andy is “.$emps[‘andy’];

Output: Employee number of andy is 1000

Directly assigning values in associative arrays

$emps[‘andy’] = 1000;
$emps[‘sam’] = 1002;
$emps[‘trisha’] = 1004;
$emps[‘sandy’] = 1006;

echo “Employee number of andy is “.$emps[‘andy’];
Output: Employee number of andy is 1000

Multidimensional Arrays:

A multidimensional array is said to be an “array of arrays”. It means this type of array contains its elements as an array, and those array elements can have their elements also an array, and so on.

Example:

<?php
$emps = array(
"andy" => array(
"emp_no" => 1000,
"designation" => "software engineer",
),

"sam" => array(
"emp_no" => 1001,
"designation" => "software engineer",
),

"trisha" => array(
"emp_no" => 1002,
"designation" => "software tester",
),

);
?>


Spread the love

Leave a Comment