Introduction
PHP provides simple ways to add new elements, update existing values, and remove unwanted elements from an array. These operations are useful when managing employee records, product lists, student details, and other changing data. In this chapter, you will practise array modification using [], array_push(), array_unshift(), unset(), array_pop(), and array_shift(). Adding and Removing Array Elements in PHP questions with solutions to help you understand the concepts.
Question 1: Add a New Element to an Indexed Array
Problem Statement
Create an array containing four programming languages. Add a new language named “JavaScript” at the end of the array using the [] operator.
PHP Solution
<?php
$languages = ["PHP", "Python", "Java", "C++"];
$languages[] = "JavaScript";
print_r($languages);
?>
Sample Output
Array
(
[0] => PHP
[1] => Python
[2] => Java
[3] => C++
[4] => JavaScript
)
Explanation
The [] operator adds a new element at the end of an indexed array. PHP automatically assigns the next available numeric index.
Concepts Covered
- Indexed arrays
- Adding array elements
- Square bracket operator
print_r()
Question 2: Add Multiple Elements Using array_push()
Problem Statement
Create an array of three product names. Add three more products using the array_push() function.
PHP Solution
<?php
$products = ["Laptop", "Mouse", "Keyboard"];
array_push($products, "Monitor", "Printer", "Speaker");
print_r($products);
?>
Sample Output
Array
(
[0] => Laptop
[1] => Mouse
[2] => Keyboard
[3] => Monitor
[4] => Printer
[5] => Speaker
)
Explanation
The array_push() function adds one or more elements to the end of an array. You can pass multiple values in a single function call.
Concepts Covered
array_push()- Adding multiple elements
- Indexed arrays
Question 3: Add an Element at the Beginning Using array_unshift()
Problem Statement
Create an array containing three city names. Add “Delhi” at the beginning of the array using array_unshift().
PHP Solution
<?php
$cities = ["Mumbai", "Jaipur", "Pune"];
array_unshift($cities, "Delhi");
print_r($cities);
?>
Sample Output
Array
(
[0] => Delhi
[1] => Mumbai
[2] => Jaipur
[3] => Pune
)
Explanation
The array_unshift() function inserts one or more elements at the beginning of an array. PHP automatically updates the numeric indexes of the existing elements.
Concepts Covered
array_unshift()- Adding elements at the beginning
- Numeric index reorganization
Question 4: Update an Existing Indexed Array Element
Problem Statement
Create an array containing five course names. Update the second course from “C Language” to “C++”.
PHP Solution
<?php
$courses = ["HTML", "C Language", "CSS", "JavaScript", "PHP"];
$courses[1] = "C++";
print_r($courses);
?>
Sample Output
Array
(
[0] => HTML
[1] => C++
[2] => CSS
[3] => JavaScript
[4] => PHP
)
Explanation
Array indexes start from 0. Therefore, the second element is stored at index 1. Assigning a new value to $courses[1] replaces the existing value.
Concepts Covered
- Updating array elements
- Array indexes
- Assigning new values
Question 5: Update an Associative Array Element
Problem Statement
Create an associative array containing employee details. Update the employee’s salary from ₹25,000 to ₹30,000.
PHP Solution
<?php
$employee = [
"name" => "Rahul",
"department" => "IT",
"salary" => 25000
];
$employee["salary"] = 30000;
print_r($employee);
?>
Sample Output
Array
(
[name] => Rahul
[department] => IT
[salary] => 30000
)
Explanation
Associative arrays use named keys instead of numeric indexes. To update a value, access the required key and assign a new value. Here, the salary associated with the "salary" key is changed.
Concepts Covered
- Associative arrays
- Updating values by key
- Key-value pairs
Question 6: Add a New Key-Value Pair to an Associative Array
Problem Statement
Create an associative array containing a student’s name, course, and city. Add a new key named "email" with an email address.
PHP Solution
<?php
$student = [
"name" => "Priya",
"course" => "PHP",
"city" => "Delhi"
];
$student["email"] = "priya@example.com";
print_r($student);
?>
Sample Output
Array
(
[name] => Priya
[course] => PHP
[city] => Delhi
[email] => priya@example.com
)
Explanation
Assigning a value to a new associative key automatically adds that key-value pair to the array. If the key already exists, its value is updated instead.
Concepts Covered
- Adding associative array elements
- Named keys
- Key-value pairs
Question 7: Remove an Element Using unset()
Problem Statement
Create an array containing five employee names. Remove the third employee using the unset() function.
PHP Solution
<?php
$employees = ["Rahul", "Amit", "Priya", "Neha", "Karan"];
unset($employees[2]);
print_r($employees);
?>
Sample Output
Array
(
[0] => Rahul
[1] => Amit
[3] => Neha
[4] => Karan
)
Explanation
The unset() function removes an element using its index or key. It does not automatically rearrange the remaining numeric indexes. Therefore, index 2 is missing in the output.
Concepts Covered
unset()- Removing array elements
- Numeric array indexes
Question 8: Remove the Last Element Using array_pop()
Problem Statement
Create an array containing five product names. Remove the last product using the array_pop() function.
PHP Solution
<?php
$products = ["Laptop", "Mouse", "Keyboard", "Monitor", "Printer"];
array_pop($products);
print_r($products);
?>
Sample Output
Array
(
[0] => Laptop
[1] => Mouse
[2] => Keyboard
[3] => Monitor
)
Explanation
The array_pop() function removes the last element from an array. It modifies the original array and reduces its number of elements by one.
Concepts Covered
array_pop()- Removing the last element
- Modifying indexed arrays
Question 9: Remove the First Element Using array_shift()
Problem Statement
Create an array containing five department names. Remove the first department using the array_shift() function.
PHP Solution
<?php
$departments = ["HR", "IT", "Sales", "Finance", "Marketing"];
array_shift($departments);
print_r($departments);
?>
Sample Output
Array
(
[0] => IT
[1] => Sales
[2] => Finance
[3] => Marketing
)
Explanation
The array_shift() function removes the first element from an array. Unlike unset(), it also reindexes the remaining numeric keys starting from 0.
Concepts Covered
array_shift()- Removing the first element
- Reindexing numeric arrays
Question 10: Update and Remove Elements in a Multidimensional Array
Problem Statement
Create a multidimensional array containing product details. Update the price of the first product and remove the third product.
PHP Solution
<?php
$products = [
[
"name" => "Laptop",
"price" => 45000
],
[
"name" => "Mouse",
"price" => 800
],
[
"name" => "Keyboard",
"price" => 1500
]
];
// Update the laptop price
$products[0]["price"] = 48000;
// Remove the keyboard record
unset($products[2]);
print_r($products);
?>
Sample Output
Array
(
[0] => Array
(
[name] => Laptop
[price] => 48000
)
[1] => Array
(
[name] => Mouse
[price] => 800
)
)
Explanation
The first product is accessed using index 0, and its price is updated through the "price" key. The unset() function then removes the third product using index 2. The remaining indexes are not automatically rearranged.
Concepts Covered
- Multidimensional arrays
- Updating nested array values
- Removing nested array elements
unset()
Key Takeaways
- Use
[]to add a new element at the end of an array. - Use
array_push()to add multiple elements at the end. - Use
array_unshift()to add elements at the beginning. - Update an existing element by assigning a new value to its index or key.
- Use
unset()to remove an element by its index or key. - Use
array_pop()to remove the last element. - Use
array_shift()to remove the first element. unset()does not automatically reindex numeric array keys.array_shift()automatically reindexes the remaining numeric keys.- You can also add, update, and remove values inside multidimensional arrays.
Frequently Asked Questions (FAQs)
1. How do you add an element to an array in PHP?
Use the square bracket operator:
$colors[] = "Blue";
PHP adds the new element at the end of the array.
2. What is the difference between array_push() and array_unshift()?
array_push() adds elements at the end of an array, while array_unshift() adds elements at the beginning.
3. How do you update an existing array element?
Access its index or key and assign a new value:
$numbers[1] = 100;
4. How do you add a new key to an associative array?
Assign a value to a new key:
$student["email"] = "student@example.com";
5. How do you remove an array element in PHP?
Use the unset() function with the required index or key:
unset($colors[1]);
6. What is the difference between unset() and array_pop()?
unset() removes an element using its index or key. array_pop() always removes the last element of an array.
7. Does unset() automatically reindex an array?
No. Removing an indexed element with unset() leaves the remaining numeric indexes unchanged.
8. Which function removes the first element of an array?
The array_shift() function removes the first element and reindexes the remaining numeric keys.
9. Can we update elements in a multidimensional array?
Yes. You can access nested elements using their indexes and keys:
$products[0]["price"] = 5000;
10. What happens when we add an existing key to an associative array?
If you assign a new value to an existing key, PHP updates its value instead of creating a duplicate key.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
