PHP Array and all it's Examples
Categories - PHP Tags - PHP   Maniruzzaman Akash   1 month ago   77   0

PHP Array and all it's Examples

What is Array in PHP

In our Data Types tutorial, we’ve shown, array is one kind of data type for almost any programming language. PHP also has that. Array is a kind of Data-Structure which allows to store multiple values in a single variable. We can store collection of data or similar types of multiple data inside array.

Practical Example 1:

In a class, there could be 40 students, or collection of students. Which students have almost same type of data but different. Like, one age, class roll no, father mother etc. So, every students have this kind of similar data. So, array is a good fit for this kind of data processing.

Practical Example 2:

In road there are three type of traffic lights. Red, Green and Yellow. List of traffic lights with a different meaning. and there could be a lots of use-case and examples of array.

How to create an array in PHP

array() is the keyword which is used to create array in PHP.

array()

There is another short form to create an array - third bracket [].

Inside the parenthesis of the array, we can put as many value as need by comma(,) separated. Like the below -

// Create an empty array of marks.
$marks = array();

// Update that array with some marks
$marks = array(80, 90, 100);

**Using [] as array create - **

// Create an empty array of marks.
$marks = [];

// Update that array with some marks
$marks = [80, 90, 100];

Types of Array in PHP

PHP has three types of array. There are -

  1. Indexed Array
  2. Associative Array
  3. Multi-dimensional Array

Indexed Array:

The array, which is used by there indexed.

Indexed Array Example:

// Create an empty array of marks.
$marks = array();

// Update that array with some marks
$marks = array(80, 90, 100);

Associative Array:

The array, which is used by key-value pair. Here is the structure -

array(
    key => value
)

Associative Array Example:

$student_data = array(
	"name" => "Jhon",
	"age" => 20,
	"result" => 3.8
);

Multi-dimensional Array:

The array, which is used by multiple array inside one array with comma separation. Here is the structure -

array(
    array(
		key => value
	),
	array(
		key => value
	),
)

Multi-dimensional Array Example:

$students = array(
    array(
		"name" => "Jhon",
		"age" => 20,
		"result" => 3.8
	),
	array(
		"name" => "Akash",
		"age" => 26,
		"result" => 2.5
	),
)

How to access array property

We can access array property by their index or key. Let’s get the structure -

  1. For index - Array index starts from 0.
  2. For Key - just key name.

Access by Index

$marks = array(10, 30, 50);

Get the First mark or 0th index mark -

echo $marks[0]; // 10

Get the Second mark or 1st index mark -

echo $marks[1]; // 30

Access by Key name

Let’s access our top student data array values. Structure -

Array variable + Third bracket Open + quotation(single or double) + Key name + quotation(single or double) + Third bracket Close


$array['key_name']

Example:

$student_data = array(
	"name" => "Jhon",
	"age" => 20,
	"result" => 3.8
);

// Get student name
echo $student_data['name']; // Jhon

// we can use
echo $student_data["age"]; // 20

Access by Loop

<?php

$students = array(
    array(
		"name" => "Jhon",
		"age" => 20,
		"result" => 3.8
	),
	array(
		"name" => "Akash",
		"age" => 26,
		"result" => 2.5
	),
);

// Access single item by a foreach loop
foreach($students as $student) {
	echo "<h2>Name: " . $student['name'] . "</h2>";
	echo "<p>Age: " . $student['age'] . "</p>";
	echo "<hr>";
}

Output:

Name: Jhon

Age: 20


Name: Akash

Age: 26


Previous
Loops in PHP - for, while, foreach, do-while
Next
PHP Array Part 2 - Array Built in PHP Functions and Some More Examples