Skip to content

php Arrays and Array Functions

Arrays and Array Functions in PHP

An array in PHP is a special variable that can hold multiple values in a single variable. Arrays are used for storing and manipulating collections of data efficiently.


1. Types of Arrays in PHP

PHP supports three types of arrays:

  1. Indexed Arrays – Arrays with numeric keys (default).
  2. Associative Arrays – Arrays with named (string) keys.
  3. Multidimensional Arrays – Arrays containing other arrays.

2. Indexed Arrays

An indexed array is a list where each element has an automatically assigned numeric index, starting from 0.

2.1 Declaring an Indexed Array

<?php

$fruits = array(“Apple”, “Banana”, “Cherry”);

$colors = [“Red”, “Green”, “Blue”];

?>

2.2 Accessing Elements

<?php

echo $fruits[0]; // Output: Apple

?>

2.3 Looping Through an Indexed Array

Using for Loop

<?php

for ($i = 0; $i < count($fruits); $i++) {

    echo $fruits[$i] . “<br>”;

}

?>

Using foreach Loop

<?php

foreach ($fruits as $fruit) {

    echo $fruit . “<br>”;

}

?>


3. Associative Arrays

An associative array uses named keys instead of numeric indexes.

3.1 Declaring an Associative Array

<?php

$person = array(

    “name” => “John”,

    “age” => 30,

    “city” => “New York”

);

?>

3.2 Accessing Elements

<?php

echo $person[“name”]; // Output: John

?>

3.3 Looping Through an Associative Array

<?php

foreach ($person as $key => $value) {

    echo “$key: $value <br>”;

}

?>


4. Multidimensional Arrays

A multidimensional array contains arrays as elements.

4.1 Declaring a Multidimensional Array

<?php

$students = array(

    array(“John”, 25, “A”),

    array(“Alice”, 22, “B”),

    array(“Bob”, 24, “C”)

);

?>

4.2 Accessing Elements

<?php

echo $students[0][0]; // Output: John

?>

4.3 Looping Through a Multidimensional Array

<?php

for ($i = 0; $i < count($students); $i++) {

    for ($j = 0; $j < count($students[$i]); $j++) {

        echo $students[$i][$j] . ” “;

    }

    echo “<br>”;

}

?>


Array Functions in PHP

PHP provides built-in functions to manipulate arrays efficiently.

5. Array Creation and Modification

5.1 Creating an Array (array())

<?php

$numbers = array(1, 2, 3, 4, 5);

?>

5.2 Adding Elements (array_push(), $array[] notation)

<?php

$fruits = [“Apple”, “Banana”];

array_push($fruits, “Cherry”); // Adds to the end

$fruits[] = “Mango”; // Alternative method

print_r($fruits);

?>

5.3 Removing Elements (array_pop(), array_shift())

<?php

array_pop($fruits);  // Removes last element

array_shift($fruits); // Removes first element

?>

5.4 Removing Specific Elements (unset())

<?php

unset($fruits[1]); // Removes element at index 1

?>


6. Sorting Arrays

6.1 Sorting Indexed Arrays

Ascending (sort())

<?php

$numbers = [3, 1, 5, 2, 4];

sort($numbers);

print_r($numbers); // Output: [1, 2, 3, 4, 5]

?>

Descending (rsort())

<?php

rsort($numbers);

print_r($numbers); // Output: [5, 4, 3, 2, 1]

?>

6.2 Sorting Associative Arrays

By Value (asort(), arsort())

<?php

$ages = [“John” => 30, “Alice” => 25, “Bob” => 28];

asort($ages);  // Sort by values in ascending order

arsort($ages); // Sort by values in descending order

?>

By Key (ksort(), krsort())

<?php

ksort($ages);  // Sort by keys in ascending order

krsort($ages); // Sort by keys in descending order

?>


7. Searching in Arrays

7.1 Check if an Element Exists (in_array())

<?php

$fruits = [“Apple”, “Banana”, “Cherry”];

echo in_array(“Banana”, $fruits) ? “Found” : “Not Found”; // Output: Found

?>

7.2 Find Element Index (array_search())

<?php

echo array_search(“Cherry”, $fruits); // Output: 2

?>

7.3 Get Keys (array_keys())

<?php

$person = [“name” => “John”, “age” => 30];

print_r(array_keys($person)); // Output: [“name”, “age”]

?>

7.4 Get Values (array_values())

<?php

print_r(array_values($person)); // Output: [“John”, 30]

?>


8. Filtering and Merging Arrays

8.1 Filtering an Array (array_filter())

<?php

$numbers = [1, 2, 3, 4, 5];

$evenNumbers = array_filter($numbers, function($num) {

    return $num % 2 == 0;

});

print_r($evenNumbers); // Output: [2, 4]

?>

8.2 Merging Arrays (array_merge())

<?php

$arr1 = [“Apple”, “Banana”];

$arr2 = [“Cherry”, “Mango”];

$merged = array_merge($arr1, $arr2);

print_r($merged); // Output: [“Apple”, “Banana”, “Cherry”, “Mango”]

?>


9. Splitting and Chunking Arrays

9.1 Splitting an Array into Chunks (array_chunk())

<?php

$numbers = [1, 2, 3, 4, 5, 6];

$chunks = array_chunk($numbers, 2);

print_r($chunks); // Output: [[1, 2], [3, 4], [5, 6]]

?>

9.2 Slicing an Array (array_slice())

<?php

$sliced = array_slice($numbers, 2, 3);

print_r($sliced); // Output: [3, 4, 5]

?>


10. Conclusion

  • Arrays in PHP can be indexed, associative, or multidimensional.
  • Built-in functions like sort(), array_merge(), array_filter(), and array_search() help manage data efficiently.
  • Associative arrays allow key-value pair storage.
  • Multidimensional arrays store complex data structures.

Understanding arrays is essential for handling data dynamically in PHP applications