PHP array functions with examples

1. How to create an array in PHP?

Question: Create an array of fruits containing “Apple,” “Banana,” and “Orange.”

Solution:

$fruits = ["Apple", "Banana", "Orange"];
print_r($fruits);

2. How to add an element to an array?

Question: Add “Grapes” to the $fruits array.

Solution:

$fruits[] = "Grapes";
print_r($fruits);

3. How to remove an element from an array?

Question: Remove “Banana” from the $fruits array.

Solution:

unset($fruits[1]);
print_r($fruits);

4. How to count elements in an array?

Question: Count the total number of elements in $fruits.

Solution:

$count = count($fruits);
echo "Total fruits: $count";

5. How to merge two arrays?

Question: Merge two arrays, $fruits and [“Mango”, “Pineapple”].

Solution:

$newFruits = array_merge($fruits, ["Mango", "Pineapple"]);
print_r($newFruits);

6. How to sort an array in ascending order?

Question: Sort the $fruits array alphabetically.

Solution:

sort($fruits);
print_r($fruits);

7. How to reverse an array?

Question: Reverse the order of elements in $fruits.

Solution:

$reversedFruits = array_reverse($fruits);
print_r($reversedFruits);

8. How to check if a value exists in an array?

Question: Check if “Apple” exists in $fruits.

Solution:

if (in_array("Apple", $fruits)) {
    echo "Apple is in the array.";
} else {
    echo "Apple is not in the array.";
}

9. How to find the index of an element in an array?

Question: Find the index of “Orange” in $fruits.

Solution:

$index = array_search("Orange", $fruits);
echo "Index of Orange: $index";

10. How to loop through an array?

Question: Print each fruit from the $fruits array.

Solution:

foreach ($fruits as $fruit) {
    echo $fruit . "\n";
}

11. How to create an associative array?

Question: Create an associative array for a person’s details (name, age, and email).

Solution:

$person = ["name" => "Dharmender Singh", "age" => 25, "email" => "dharmendersingh@blogshub.co.in"];
print_r($person);

12. How to access an element in an associative array?

Question: Access the “email” value from the $person array.

Solution:

echo $person["email"];

13. How to update an element in an array?

Question: Update “age” in the $person array to 26.

Solution:

$person["age"] = 26;
print_r($person);

14. How to check if a key exists in an array?

Question: Check if “phone” exists in $person.

Solution:

if (array_key_exists("phone", $person)) {
    echo "Phone exists.";
} else {
    echo "Phone does not exist.";
}

15. How to filter an array?

Question: Filter an array of numbers to get only even numbers.

Solution:

$numbers = [1, 2, 3, 4, 5, 6];
$evenNumbers = array_filter($numbers, fn($num) => $num % 2 === 0);
print_r($evenNumbers);

16. How to sum values in an array?

Question: Calculate the sum of all numbers in an array.

Solution:

$sum = array_sum($numbers);
echo "Sum: $sum";

17. How to remove duplicate values from an array?

Question: Remove duplicates from an array [1, 2, 2, 3, 4, 4, 5].

Solution:

$uniqueNumbers = array_unique([1, 2, 2, 3, 4, 4, 5]);
print_r($uniqueNumbers);

18. How to slice an array?

Question: Extract the first two elements from $fruits.

Solution:

$slicedFruits = array_slice($fruits, 0, 2);
print_r($slicedFruits);

19. How to combine two arrays?

Question: Combine [“name”, “age”] and [“John”, 25] into an associative array.

Solution:

$keys = ["name", "age"];
$values = ["Dharmender", 25];
$combined = array_combine($keys, $values);
print_r($combined);

20. How to sort an associative array by values?

Question: Sort the $person array by values in ascending order.

Solution:

asort($person);
print_r($person);

These practical examples cover common PHP array operations and are helpful for interviews

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *