codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

23 / 56 topics
19Arrays in PHP20Indexed Arrays21Associative Arrays22Multidimensional Arrays23Array Functions
Tutorials/PHP/Array Functions
🐘PHP

Array Functions

Updated 2026-05-15
10 min read

Array Functions

Introduction

In PHP, arrays are one of the most versatile and powerful data structures. They allow you to store multiple values in a single variable, making them essential for various tasks such as handling lists, collections, and more. PHP provides a wide range of built-in functions to manipulate arrays efficiently. This tutorial will cover some of the most commonly used array functions, providing both theoretical explanations and practical examples.

Concept

Arrays in PHP can be either indexed (numeric) or associative. Indexed arrays use numeric indices starting from 0, while associative arrays use named keys that you assign to them. Understanding these types is crucial before diving into array functions.

Common Array Functions

  1. array_push(): Adds one or more elements to the end of an array.
  2. array_pop(): Removes and returns the last element of an array.
  3. array_shift(): Removes and returns the first element of an array.
  4. array_unshift(): Adds one or more elements to the beginning of an array.
  5. in_array(): Checks if a value exists in an array.
  6. array_search(): Searches for a given value in an array and returns the first corresponding key if successful.
  7. array_keys(): Returns all the keys or a subset of the keys of an array.
  8. array_values(): Returns all the values of an array.
  9. array_reverse(): Reverses the order of elements in an array.
  10. sort() and rsort(): Sort arrays in ascending or descending order, respectively.

Examples

Let's explore some practical examples to understand how these functions work.

Example 1: Adding Elements with array_push()

$fruits = ["apple", "banana"];
array_push($fruits, "cherry", "date");
print_r($fruits);
Output
Array
(
  [0] => apple
  [1] => banana
  [2] => cherry
  [3] => date
)

Example 2: Removing Elements with array_pop()

$fruits = ["apple", "banana", "cherry"];
$lastFruit = array_pop($fruits);
echo $lastFruit; // Outputs: cherry
print_r($fruits);
Output
Array
(
  [0] => apple
  [1] => banana
)

Example 3: Searching for a Value with in_array()

$fruits = ["apple", "banana", "cherry"];
if (in_array("banana", $fruits)) {
    echo "Banana is in the array!";
} else {
    echo "Banana is not in the array.";
}

<OutputBlock>
{`Banana is in the array!`}
</OutputBlock>

### Example 4: Associative Arrays and `array_search()`

```php
$person = ["name" => "John", "age" => 30, "city" => "New York"];
$key = array_search("New York", $person);
echo $key; // Outputs: city
Output
city

Example 5: Reversing an Array with array_reverse()

$numbers = [1, 2, 3, 4, 5];
$reversedNumbers = array_reverse($numbers);
print_r($reversedNumbers);
Output
Array
(
  [0] => 5
  [1] => 4
  [2] => 3
  [3] => 2
  [4] => 1
)

What's Next?

After mastering array functions, the next step is to explore string manipulation in PHP. Strings are another fundamental data type in PHP, and understanding how to work with them will enhance your ability to process and manipulate text effectively.

Stay tuned for more tutorials on PHP basics and advanced topics!


PreviousMultidimensional ArraysNext Strings in PHP

Recommended Gear

Multidimensional ArraysStrings in PHP