Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 28

PHP Array Functions (with Output)

In this lesson: Most-used PHP array functions — sort, usort, array_map, array_filter, array_merge, in_array — with output.

Why array functions

PHP has dozens of built-in array functions that do common tasks — sorting, searching, transforming, combining — in one line, so you rarely need to write loops for them. Learning a handful of the most useful ones makes your code shorter and clearer.

💡 Some functions change the array in place (like sort); others return a new array (like array_map). Know which is which.

count, sort, reverse

<?php
  $n = [3, 1, 2];
  echo count($n);        // 3
  sort($n);              // [1, 2, 3]
  print_r($n);
  $r = array_reverse($n); // [3, 2, 1]
?>
3
Array ( [0] => 1 [1] => 2 [2] => 3 )

count gives the size, sort orders values (changing the array in place), and array_reverse returns a reversed copy.

<?php
  $fruits = ["apple", "mango", "banana"];
  var_dump(in_array("mango", $fruits));  // true
  echo array_search("banana", $fruits);  // 2 (its index)
?>
bool(true)
2

in_array tells you if a value exists (true/false); array_search returns its key/index, or false if not found.

array_map

Apply a function to every item and get a new array back.

<?php
  $nums = [1, 2, 3];
  $doubled = array_map(function($x) { return $x * 2; }, $nums);
  print_r($doubled);
?>
Array ( [0] => 2 [1] => 4 [2] => 6 )

Every number is doubled. The original $nums stays the same — map returns a fresh array of results.

array_filter

Keep only items that pass a test.

<?php
  $nums = [1, 2, 3, 4, 5, 6];
  $even = array_filter($nums, function($x) { return $x % 2 == 0; });
  print_r($even);
?>
Array ( [1] => 2 [3] => 4 [5] => 6 )

Only even numbers remain. Note the keys stay from the original — use array_values() if you want them renumbered from 0.

array_merge and array_keys

<?php
  $a = ["x" => 1, "y" => 2];
  $b = ["y" => 9, "z" => 3];
  print_r(array_merge($a, $b));   // y becomes 9
  print_r(array_keys($a));        // ["x", "y"]
?>
Array ( [x] => 1 [y] => 9 [z] => 3 )
Array ( [0] => x [1] => y )

array_merge combines arrays (later string keys overwrite earlier ones); array_keys returns all the keys.

Common mistakes

  • Expecting sort to return a value. sort() changes the array in place and returns true/false, not the sorted array.
  • Forgetting filter keeps original keys. Use array_values() to renumber if needed.
  • Checking in_array with loose match. Pass true as the third argument for a strict (type-aware) check.
  • Assuming array_map changes the original. It returns a new array; the original is untouched.

Summary

  • count, sort, array_reverse for size and order.
  • in_array and array_search to find values.
  • array_map transforms every item into a new array.
  • array_filter keeps items that pass a test.
  • array_merge combines arrays; array_keys lists keys.

Next, review the basics in PHP arrays, or loop over them with PHP loops.

Frequently Asked Questions

What does array_map do in PHP?
array_map applies a function to every item of an array and returns a new array of the results. For example, doubling every number or trimming every string. The original array is not changed.
What is the difference between array_map and array_filter?
array_map transforms every item and keeps the same count. array_filter keeps only the items that pass a test and can return fewer items. Map changes values; filter selects values.
How do I sort an array in PHP?
Use sort() for values in ascending order, rsort() for descending, and asort()/ksort() to sort associative arrays by value or key. Most sort functions change the array in place.
How do I check if a value exists in an array?
Use in_array("x", $arr) which returns true or false, or array_search("x", $arr) which returns the key/index of the value (or false if not found).
Does array_merge keep keys?
For numbered arrays, array_merge re-indexes from 0. For string keys, later values overwrite earlier ones with the same key. Knowing this avoids surprises when combining arrays.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicDate and Time Your progress: 28 / 50 (56%) · Lesson 29 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free