MasterPHP.in
PHP Tutorial

PHP Array Functions & Operations


In the previous lesson, you learned how arrays store data. But in real applications, you rarely just store data — you need to search, sort, filter, and transform it.

PHP provides powerful built-in functions to perform these operations efficiently. In this lesson, you’ll learn the most important array functions and how to use them in real-world scenarios.


Why Array Functions Matter

Imagine working with:

  • a list of products
  • user data from a database
  • API responses

You’ll constantly need to:

  • check if a value exists
  • sort data
  • merge arrays
  • remove or transform elements

Array functions help you do all of this with minimal code.


Adding & Removing Elements

Add Element — array_push()

$colors = ["red", "blue"];

array_push($colors, "green");

print_r($colors);

Output:

Array ( [0] => red [1] => blue [2] => green )

Remove Last Element — array_pop()

array_pop($colors);

Remove First Element — array_shift()

array_shift($colors);

Add to Beginning — array_unshift()

array_unshift($colors, "yellow");


Searching in Arrays

Check Value — in_array()

$colors = ["red", "blue", "green"];

$result = in_array("red", $colors);
var_dump($result);

Output:

bool(true)

Search Key — array_search()

echo array_search("blue", $colors);

Output:

1


Sorting Arrays

Indexed Array Sorting

sort($colors);   // Ascending
rsort($colors);  // Descending

Associative Array Sorting

$user = ["name" => "Rohan", "age" => 22];

ksort($user); // Sort by key
asort($user); // Sort by value


Merging & Combining Arrays

Merge Arrays — array_merge()

$a = ["red", "blue"];
$b = ["green", "yellow"];

$result = array_merge($a, $b);

print_r($result);

Output:

Array ( [0] => red [1] => blue [2] => green [3] => yellow )


Transforming Arrays

Get Keys — array_keys()

$user = ["name" => "Rohan", "age" => 22];

print_r(array_keys($user));

Output:

Array ( [0] => name [1] => age )

Get Values — array_values()

$colors = ["red", "blue", "green"];

unset($colors[1]);

print_r($colors);

Output (after unset):

Array ( [0] => red [2] => green )

👉 Notice the missing index.

Fix Re-indexing

$colors = array_values($colors);

print_r($colors);

Output:

Array ( [0] => red [1] => green )


Debugging Arrays (Important)

print_r() — Readable Output

print_r($colors);

👉 Used to quickly inspect array structure.

var_dump() — Detailed Output

var_dump($colors);

👉 Shows type + value (useful for debugging).


Loop-Based Operations (Important)

Example: Calculate Total Price

$products = [
    ["name" => "Laptop", "price" => 50000],
    ["name" => "Phone", "price" => 20000]
];

$total = 0;

foreach ($products as $product) {
    $total += $product["price"];
}

echo "Total: ₹" . $total;

Output:

Total: ₹70000


Common Mistakes Beginners Make

1. Using Wrong Data Structure

array_push($colors, ["black"]);

Problem:

Adds nested array instead of a string.

Correct:

array_push($colors, "black");

2. Forgetting Functions Modify Original Array

sort($colors);

👉 This changes the original array permanently.

3. Confusing array_merge() with +

$a + $b;

Problem:

Does not merge arrays correctly.

Correct:

array_merge($a, $b);


Practice Exercise

Task 1 (Easy)

Create this array and print the second fruit:

$fruits = ["Mango", "Apple", "Orange"];

Expected Output:

Apple

Task 2 (Medium)

Create an associative array and loop through it to print all keys and values.

Expected Output:

name: Rohan age: 22

Task 3 (Hard)

Create an array of 3 students with name and marks. Print only students who scored above 60.

Expected Output:

Rohan
Amit


Summary

In this lesson, you learned how to perform operations on arrays using built-in PHP functions. You explored adding, removing, searching, sorting, and transforming arrays.

These operations are essential for handling real-world data efficiently.

Share this tutorial