Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is an Indexed Array?
3.
Declaring Indexed Arrays
3.1.
Example 1
3.2.
Example 2
4.
Accessing Elements of Indexed Arrays
4.1.
Example 1
4.2.
Output 1
4.3.
Example 2
4.4.
Output 2
5.
Modifying Elements of Indexed Arrays
5.1.
Example 1
5.2.
Output 1
5.3.
Example 2
5.4.
Output 2
6.
Iterating Through Indexed Arrays
6.1.
Example 1
6.2.
Output 1
6.3.
Example 2
6.4.
Output 2
7.
Array Operations
7.1.
Counting Elements in an Indexed Array
7.1.1.
Example
7.1.2.
Output
7.2.
Searching in an Indexed Array
7.2.1.
Example
7.2.2.
Output
7.3.
Adding and Removing Elements
7.3.1.
Example
7.3.2.
Output
7.4.
Slice and Splice
7.4.1.
Example using array_slice()
7.4.2.
Output
7.4.3.
Example using array_splice()
7.4.4.
Output
7.5.
Merging Indexed Arrays
7.5.1.
Example
7.5.2.
Output
8.
Frequently Asked Questions
8.1.
What is the difference between an indexed array and an associative array in PHP?
8.2.
Can an indexed array have string keys in PHP?
8.3.
How can I check if a variable is an indexed array in PHP?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

PHP Indexed Arrays

Author Sanchit Kumar
0 upvote

Introduction

In PHP, arrays are versatile data structures that allow you to store multiple values under a single variable. Indexed arrays are the most basic and commonly used among various types of arrays.

PHP Indexed Arrays

This article on PHP indexed arrays will guide you through the concept of indexed arrays in PHP, from declaring and accessing elements to performing essential operations. Let us start by learning what an indexed array is.

What is an Indexed Array?

An indexed array is a collection of elements in which each element is associated with a numeric index. The index serves as a reference to access and manipulate the corresponding element in the array. Indexed arrays in PHP can be either explicitly declared or dynamically populated.

Declaring Indexed Arrays

To declare an indexed array in PHP, you enclose a list of elements within square brackets ([]), separating them with commas.

Example 1

<?php
$courses = ['C++', 'Java', 'PHP'];
?>
You can also try this code with Online PHP Compiler
Run Code

Example 2

<?php
$names = ['Juhi', 'Kanishk', 'Shiv', 'Kartik'];
?>
You can also try this code with Online PHP Compiler
Run Code

Accessing Elements of Indexed Arrays

You can access the elements of an indexed array using their numeric indices, which start from 0 for the first element, 1 for the second element, and so on.

Example 1

<?php
$courses = ['C++', 'Java', 'PHP'];
echo $courses[0];
?>
You can also try this code with Online PHP Compiler
Run Code

Output 1

C++

Example 2

<?php
$names = ['Juhi', 'Kanishk', 'Shiv', 'Kartik'];
echo $names[1];
?>
You can also try this code with Online PHP Compiler
Run Code

Output 2

Kanishk

Modifying Elements of Indexed Arrays

You can modify the elements of an indexed array by assigning new values to specific indices.

Example 1

<?php
$courses = ['C++', 'Java', 'PHP'];
$courses[1] = 'Python';
print_r($courses);
?>
You can also try this code with Online PHP Compiler
Run Code

Output 1

Array
(
	[0] => C++
	[1] => Python
	[2] => PHP
)

Example 2

<?php
$names = ['Juhi', 'Kanishk', 'Shiv', 'Kartik'];
$names[2] = 'Shreya';
print_r($names);
?>
You can also try this code with Online PHP Compiler
Run Code

Output 2

Array
(
	[0] => Juhi
	[1] => Kanishk
	[2] => Shreya
	[3] => Kartik
)

Iterating Through Indexed Arrays

You can use loops, such as 'for' and 'foreach', to iterate through the elements of an indexed array.

Example 1

<?php
$courses = ['C++', 'Java', 'PHP'];
$length = count($courses);
for ($i = 0; $i < $length; $i++) {
	echo $courses[$i] . " ";
}
?>
You can also try this code with Online PHP Compiler
Run Code

Output 1

C++ Java PHP

Example 2

<?php
$names = ['Juhi', 'Kanishk', 'Shiv', 'Kartik'];
foreach ($names as $name) {
	echo $name . " ";
}
?>
You can also try this code with Online PHP Compiler
Run Code

Output 2

Juhi Kanishk Shiv Kartik

Array Operations

Indexed arrays in PHP support various operations that allow you to manipulate their content effectively.

Counting Elements in an Indexed Array

You can determine the number of elements in an indexed array using the 'count()' function or the 'sizeof()' function.

Example

<?php
$courses = ['C++', 'Java', 'PHP'];
echo count($courses);
?>
You can also try this code with Online PHP Compiler
Run Code

Output

3

Searching in an Indexed Array

You can search for a specific value in an indexed array using the 'array_search()' function.

Example

<?php
$courses = ['C++', 'Java', 'PHP'];
$searchedIndex = array_search('Java', $courses);
if ($searchedIndex !== false) {
	echo "Found at index: " . $searchedIndex;
} 
else {
	echo "Not found.";
}
?>
You can also try this code with Online PHP Compiler
Run Code

Output

Found at index: 1

Adding and Removing Elements

You can add elements to the end of an indexed array using the '[]' notation or the 'array_push()' function. You can use the 'array_pop()' function to remove elements.

Example

<?php
$names = ['Shiv', 'Kanishk'];

// Adds 'Python' to the end of the array
$names[] = 'Juhi';

// Delete the last element of the array
array_pop($names);

print_r($names);
?>
You can also try this code with Online PHP Compiler
Run Code

Output

Array
(
	[0] => Shiv
	[1] => Kanishk
)

Slice and Splice

You can extract a portion of an indexed array using the 'array_slice()' function. You can use the 'array_splice()' function to remove and replace elements in an array.

Example using array_slice()

<?php
$names = ['Shiv', 'Kanishk', 'Juhi', 'Gaurav'];
$slicedNames = array_slice($names, 1, 3);
print_r($slicedNames);
?>
You can also try this code with Online PHP Compiler
Run Code

Output

Array
(
	[0] => Kanishk
	[1] => Juhi
	[2] => Gaurav
)

Example using array_splice()

<?php
$names = ['Juhi', 'Kanishk', 'Shiv', 'Kartik', 'Shreya'];
array_splice($names, 2, 2, ['Saurabh', 'Rahul']);
print_r($names);
?>

Output

Array
(
	[0] => Juhi
	[1] => Kanishk
	[2] => Saurabh
	[3] => Rahul
	[4] => Shreya
)

Merging Indexed Arrays

You can merge two or more indexed arrays into a single array using the 'array_merge()' function.

Example

<?php
$courses1 = ['C++', 'Java'];
$courses2 = ['PHP', 'Python', 'JavaScript'];
$allCourses = array_merge($courses1, $courses2);
print_r($allCourses);
?>

Output

Array
(
	[0] => C++
	[1] => Java
	[2] => PHP
	[3] => Python
	[4] => JavaScript
)

Frequently Asked Questions

What is the difference between an indexed array and an associative array in PHP?

In an indexed array, elements are associated with numeric indices (0, 1, 2, ...), while in an associative array, elements are associated with user-defined keys. Indexed arrays are appropriate when elements need to be accessed sequentially, whereas associative arrays are used when elements need to be accessed using custom keys for easier identification.

Can an indexed array have string keys in PHP?

No, indexed arrays in PHP can only have numeric indices starting from 0. If you use a string as an index, PHP will automatically treat it as an associative array.

How can I check if a variable is an indexed array in PHP?

You can use the 'is_array()' function along with a check for numeric indices to determine if a variable is an indexed array.

Conclusion

In this article, we discussed PHP Indexed Arrays. We learnt Indexed arrays are fundamental data structures in PHP, allowing you to store multiple values in a single variable. Understanding indexed arrays and their manipulation will enable you to efficiently handle data collection in your PHP applications. Alright! So now that you have learnt about PHP Indexed Arrays, you can also refer to similar articles.

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!

Live masterclass