PHP Array Functions

11/22/2025

PHP Array Functions array_pop

Go Back

PHP Array Functions: A Complete Guide for Beginners

PHP provides a rich collection of built-in array functions that make it easy to work with, manipulate, and transform arrays. Whether you're working with indexed, associative, or multidimensional arrays, these functions help you perform tasks like sorting, merging, searching, slicing, filtering, and more.

This guide covers the most important PHP array functions with simple explanations and developer-friendly examples.


PHP Array Functions array_pop

1. count() – Count Array Elements

Returns the number of elements in an array.

$fruits = ["Apple", "Banana", "Orange"];
echo count($fruits); // 3

2. array_push() – Add Elements at the End

Adds one or more elements to the end of an array.

$colors = ["Red", "Green"];
array_push($colors, "Blue", "Yellow");

3. array_pop() – Remove Last Element

Removes and returns the last element of an array.

$numbers = [10, 20, 30];
$last = array_pop($numbers); // 30

4. array_shift() – Remove First Element

Removes and returns the first element.

$names = ["Amit", "Ravi", "Sneha"];
$first = array_shift($names); // Amit

5. array_unshift() – Add Elements at the Beginning

$names = ["Ravi", "Sneha"];
array_unshift($names, "Amit");

6. sort() – Sort Array in Ascending Order

Useful for indexed arrays.

$fruits = ["Mango", "Apple", "Banana"];
sort($fruits);

7. rsort() – Sort in Descending Order

$numbers = [50, 10, 30];
rsort($numbers);

8. asort() – Sort Associative Array by Value (Ascending)

$marks = ["Amit" => 80, "Sneha" => 90];
asort($marks);

9. ksort() – Sort Associative Array by Key (Ascending)

$marks = ["Rahul" => 85, "Amit" => 90];
ksort($marks);

10. in_array() – Check If Value Exists

$colors = ["Red", "Blue", "Green"];
if (in_array("Blue", $colors)) echo "Found";

11. array_key_exists() – Check If Key Exists

$user = ["name" => "Shubham", "city" => "Pune"];
if (array_key_exists("city", $user)) echo "Key exists";

12. array_keys() – Get All Keys

$user = ["name" => "Amit", "age" => 25];
print_r(array_keys($user));

13. array_values() – Get All Values

print_r(array_values($user));

14. array_merge() – Merge Arrays

$a = ["Red", "Blue"];
$b = ["Green", "Yellow"];
$merged = array_merge($a, $b);

15. array_slice() – Extract Portion of Array

$fruits = ["Apple", "Banana", "Orange", "Mango"];
print_r(array_slice($fruits, 1, 2)); // Banana, Orange

16. array_splice() – Remove and Replace Elements

$fruits = ["Apple", "Banana", "Orange"];
array_splice($fruits, 1, 1, "Mango");

17. array_search() – Search Value and Return Key

$fruits = ["a" => "Apple", "b" => "Banana"];
echo array_search("Banana", $fruits); // b

18. array_unique() – Remove Duplicate Values

$numbers = [10, 20, 20, 30, 10];
print_r(array_unique($numbers));

19. implode() – Convert Array to String

$names = ["Amit", "Ravi", "Sneha"];
echo implode(", ", $names);

20. explode() – Convert String to Array

$data = "Amit,Ravi,Sneha";
print_r(explode(",", $data));

21. array_map() – Apply Function to Every Element

$numbers = [1, 2, 3];
$double = array_map(fn($n) => $n * 2, $numbers);

22. array_filter() – Filter Elements Based on Condition

$numbers = [10, 25, 30, 40];
$filtered = array_filter($numbers, fn($n) => $n > 20);

23. array_reduce() – Reduce Array to a Single Value

$numbers = [1, 2, 3, 4];
$sum = array_reduce($numbers, fn($carry, $item) => $carry + $item, 0);

Real-World Example: Shopping Cart Total

$cart = [
    ["item" => "Laptop", "price" => 55000],
    ["item" => "Mouse", "price" => 799]
];

$total = array_reduce($cart, fn($sum, $p) => $sum + $p['price'], 0);

echo "Total Price: ₹" . $total;

Conclusion

PHP offers dozens of array functions that make handling data easy, efficient, and powerful. Mastering these functions will help you write cleaner, faster, and more organized PHP code.

Table of content

  • Introduction to Apache Hive
  • Hive Architecture and Components
  • Hive Modes
  • Installation and Setup
    • Installing Hive on Linux/Windows
    • Configuring Hive with Hadoop
    • Verifying the Installation
  • Working with Hive Tables
    • Internal (Managed) Tables
    • External Tables
    • Creating Tables in Hive
    • Altering and Dropping Tables
    • Partitioning in Hive
    • Bucketing in Hive
  • HiveQL Basics
    • SELECT Queries
    • Filtering Data with WHERE
    • Sorting and Grouping Data
    • Using Joins in Hive
  • Advanced Hive Concepts
    • Partition Pruning
    • Dynamic Partitioning
    • Query Optimization in Hive
    • Working with Hive Indexes
    • ACID Transactions in Hive
  • File Formats in Hive
    • Text File
    • ORC (Optimized Row Columnar)
    • Parquet
    • Avro
    • Sequence File
  • Hive Functions
    • Built-in Functions (String, Date, Math)
    • Aggregate Functions
    • User-Defined Functions (UDFs)
  • Integrating Hive with Other Tools
    • Hive and Apache Spark
    • Hive and Pig
    • Hive and HBase
  • Hive Interview Questions
  • Best Practices in Hive
    • Performance Optimization
    • Handling Large Datasets
    • Security and Access Control
  • FAQs and Common Errors in Hive
    • Troubleshooting Hive Issues
    • Frequently Asked Questions
  • Resources and References
    • Official Hive Documentation
    • Recommended Books and Tutorials