PHP Arrays Tutorial for Beginners: Types, Syntax, Examples

PHP arrays are one of the most important data structures

Arrays are used to store values.that values are homogeneous means same data type elements are stored in arrays.every element is accessed by using index values.
Arrays index values start with 0,1,2…..and so on.these values are written in array()keyword.

For example, you can create an array using the array() function.

$variarble_name=array(element 1, element 2, element 3);
$a=array(1,2,3,4,5);
Here $variable is a.and element values are 1,2,3,4,5.these five elements belongs to integer values.
Similarly, you can also store string values in an array. .here is the declaration
$flowers=array(“rose”,”marrygold”,”sunflower“);
Variable name is flower
Element are in the string datatype.
All string elements are written with in double quotation marks.

Program for arrays in php

$fruits = array(“pineapple”,”kiwi”,”dragonfruit”);
print_r($fruits);."\n";
?>
Output 
pineapple
Kiwi
dragonfruit

Types of arrays in php

There are three types of arrays in php.

1.indexed array

 

2.associative array

 

3.multidinesional array

1.indexed array in php

Indexed arrays are used to call variable by index values.these index values start from 0.arr[0] is indicated as first element,arr[1]is indicated as second element up to the number of elements entered by the user.

Example program for indexed arrays in php

<?php
$flowers=array(“rose”,”marigold”,”hibiscus”);
echo $flowers[2];
?>
Output:
hibiscus

Explanation:

Here array element are stored with index values.those are
arr[0]=rose
arr[1]=marigold
arr[2]=hibiscus
Therefore, indexed arrays are easy to use for beginners.

2.associative indexes in php

Associate indexes are used to store the
Keys with names.this associative index takes keys with key values in that same array declaration .

Syntax for associate index

$a=array(key1=>value1,key2=>value2,key3=>value3);
This is the syntax for associate index in php.now we are writting example program on it.
<?php
$student=array(“name”=>”pandu”,”roll no”=>23,”branch”=>”BCA”,”gender”=>”male”);
echo $student[name].”\n”;
echo $student[branch].”\n”;
?>
Output:
pandu
BCA

Multidimensional arrays in php

Multidimensional arrays are like nested loops.nested loops means loop inside another loop like that only one array inside another array is used in multi dimensional arrays.these arrays stores multiple values that stored in matrix form.array elements are called by array indexes with rows and columns.we need to select which row and which column element by index number.

Syntax for multidimensional arrays

$a=array(array(value1,value2), array(value3,value4),array(value5,value6));
Example program for multi dimensional arrays in php:
<?php
$student= array(
    array(“Ramu”, 20),
    array(“siri”, 40),
    array(“Rajini”, 25)
);
echo “Name: ” . $student[0][0] . “\n”;
echo “Marks: ” . $students[2][1];
?>
Output:
Name: Ramu
Marks: 25.
Array operators in php:There are six operators in arrays.There are mainly used to compare two array with different purposes.Those operators are used to perform different operations in arrays.Array operators are important concept in php.

Those six operators are

1.+(Unary operator)
2.==(Equality operator)
3.===(Identity operator)
4.!=(Not equal to)
5.!==(Not identical)
6.<>(Not equal to)

1.unary(+)

Unary operator is used to combine one array with another array.this operator works like a concatenation.the only difference is concatenation is used in statements and this unary is used in arrays.if two arrays are stored same key values we can use this unary for combine that elements.

Example program

<?php
$a=array(“A”=>”apple”,”B”=>”Booklet”,”c”=>”carona”);
$b=array(“D”=>”danger”);
$res= $a+$b;
echo print_r($res);
?>
Output:
array(
[A] => apple
[B]=>Booklet
[C]=>carona
[D]=>danger
)

2.==(Equality)

Equality is used to check whether two arrays are having same elements means element values are not .
Example program:
<?php
$name1=array(1,2,3);
$name2=array(1,2,3);
var_dump($name1 == $name2);
?>
Output:
bool(true)

Example 2

<?php
$name1=array(1,2,3);
$name2=array(1,2,3,4,5);
var_dump($name1 == $name2);
?>
Output:
bool(false)

Explanation

In this two programs there are checking array element are same are not .if elements are same they give true variable type is boolean.if elements are not matched it gives false.
However, associative arrays use named keys instead of numeric indexes.

3.===(Identity)

Identity operator is used for checking two arrays.that key values and keys order must be in same order.if the order is same it gives true.otherwise false .in Identity operator and equality operator both only gives Boolean data type with true or false values.

Example program for Identity

<?php
$num1 = array(12,21,31);
$num2=(12,31,21);
$res=($num1===$num2);
var_dump($res);
?>
Output:
bool(false)
Program 2:
<?php
$num1 = array(12,21,31);
$num2=(12,21,31);
$res=($num1===$num2);
var_dump($res);
?>
Output:
bool(true)

4.not equal(!=)

Not equal operators is used to check whether two arrays are not matched ot not.
Program:
<?php
$a=(1,2,3,4,5,6);
$b=(5,6,7,8);
var_dump($a != $b);
?>
Output:
bool(true)

5.not equal (<>)

Not equal operators is used to check whether two arrays are not matched ot not.
Program:
<?php
$a=(1,2,3,4,5,6);
$b=(5,6,7,8);
var_dump($a <>$b);
?>
Output:
bool(true)

6.not Identical (!==)

Not identical operator are used to check whether two array element and values are not same or same.if keys and values same means identical.if keys and values are not matched means not identical.

Program for not identical

<?php
$a=(1,3,3);
$b=(2,3,4);
var_duml($a != $b);
?>
Output:
bool(true).

Frequently Asked Questions

1.What is an array in PHP?

A.array is a variable.it is used for storing element of same datatype.multiple values in single variable declaration possible in arrays.

2What are the different types of arrays in PHP?

Answer:
indexred array, associative array, multidimensional arrays.

3.How do you create an indexed array in PHP?

Answer:
index values are start with 0.
<?php
$a=(2,5,8);
echo $a[1];
?>
Output:
5

4 What is an associative array in PHP? Explain with an example.

Answer:
associaive array are used to store keys with their key values in the array.
Example:
<?php
$name=(“prabhas”=>”darling”,”mahesh”=>”businessman”);
echo $name[0];
?>
Output:
Prabhas=>darling

5.What is a multidimensional array in PHP?

Answer:
multidimensional arrays are used to store values in matrix.values are taken in the form of rows and columns with index numbers.

6.What are operators in PHP?

Answer:
operators are important in php.these are used to check different operations by using this operators

7.What are the different types of operators in PHP?

Answer:
There are six types of operators.
Identical,unary, equality,not equal,not equal to,not identical.

8.What is the difference between == and === operators in PHP?

Answer:
== It checks same keyvalues are their in two arrays.===is used to check same key values with same order in the array.

Conclusion

Yes. In addition, PHP arrays are easy for beginners to learn.

1 thought on “PHP Arrays Tutorial for Beginners: Types, Syntax, Examples”

Leave a Comment