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 DSA, Competitive Programming, System 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!