Creating PHP Indexed Arrays
In PHP, indexed arrays are created by assigning values to an array with automatic numeric indexing, starting at zero. You can define an indexed array using the array() function or with short array syntax [].
Syntax
To create an indexed array using the array() function, you simply pass a comma-separated list of values to the function, enclosed in parentheses. Here is an example:
$myArray = array("apple", "banana", "orange", "grape");
This creates an indexed array called $myArray that contains four elements: "apple", "banana", "orange", and "grape". Each element is assigned a unique index, starting from 0 for the first element and increasing by 1 for each subsequent element.
Using square brackets:
$array_name = [value1, value2, value3, ..., valueN];
Alternatively, you can create an indexed array using square brackets to define the elements. Here is an example:
$myArray = ["apple", "banana", "orange", "grape"];
This creates the same indexed array as the previous example but uses a different method.
$array_name = array("value1", "value2", "value3"); or $array_name = ["value1", "value2", "value"];.
Each element is automatically assigned an index starting from zero. Access elements by their index using $array_name[index], where "index" is the position of the value in the array. This straightforward syntax allows for easy manipulation and retrieval of array elements.
Declaring PHP 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 CodeExample 2
<?php
$names = ['Juhi', 'Kanishk', 'Shiv', 'Kartik'];
?>

You can also try this code with Online PHP Compiler
Run CodeHow to Access 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 CodeOutput 1
C++
Example 2
<?php
$names = ['Juhi', 'Kanishk', 'Shiv', 'Kartik'];
echo $names[1];
?>

You can also try this code with Online PHP Compiler
Run CodeOutput 2
Kanishk
Modifying Elements of Indexed Arrays in PHP
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 CodeOutput 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 CodeOutput 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 CodeOutput 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 CodeOutput 2
Juhi Kanishk Shiv Kartik
PHP Indexed Array Operations
Indexed arrays in PHP support various operations that allow you to manipulate their content effectively.
1. 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 CodeOutput
3
2. 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 CodeOutput
Found at index: 1
3. 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 CodeOutput
Array
(
[0] => Shiv
[1] => Kanishk
)
4. 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 CodeOutput
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
)
5. 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
)
PHP Indexed Array Example
Code Implementation
<?php
// Creating an indexed array
$fruits = ["apple", "banana", "orange", "grape"];
// Accessing elements in the array
echo $fruits[0] . "\n";
echo $fruits[1] . "\n";
echo $fruits[2] . "\n";
echo $fruits[3] . "\n";
?>

You can also try this code with Online PHP Compiler
Run Code
Output
apple
banana
orange
grape
Explanation:
In this example, an indexed array $fruits is created using square brackets ([]). Each element in the array is assigned a unique index, starting from 0. By using echo statements, individual elements are accessed by their index, and printed in order:
- $fruits[0] outputs "apple",
- $fruits[1] outputs "banana",
- $fruits[2] outputs "orange", and
- $fruits[3] outputs "grape".
Indexed arrays like this allow easy access to elements by referencing their index values.
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.
Can indexed arrays hold different data types in PHP?
Yes, PHP indexed arrays can hold different data types, such as integers, strings, floats, and even other arrays. PHP arrays are flexible and can store a mix of data types within the same array.
What is the difference between an indexed array and a multidimensional array in PHP?
An indexed array in PHP is a one-dimensional array with a single set of index values, while a multidimensional array contains arrays within an array. Multidimensional arrays allow storage in rows and columns, ideal for complex data structures.
Conclusion
In this article, we learned PHP Indexed Arrays. We learned that indexed arrays are essential data structures in PHP, enabling the storage of multiple values within 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.
Happy Learning, Ninja!