Preloader

PHP array types

PHP Tutorials
codevigyaan php

PHP array types assist programmers in storing and manipulating data in an organized manner. In PHP, there are three types of arrays: indexed arrays, associative arrays, and multidimensional arrays. This makes it essential for programmers to learn about PHP array types to write clean and optimized code.

Indexed Arrays in PHP

Creating an Indexed Array

You can create an indexed array using the array function or short syntax.

<?php
$fruits = ["Apple", "Banana", "Mango"];
?>

In the above example, Apple is assigned to the index 0 position, Banana to the index 1 position, and Mango to the index 2 position.

Accessing Indexed Array Values

To access the value of an array, you need to use the index number.

<?php
echo $fruits[1];
?>

This will display Banana.

Looping Through Indexed Arrays

A foreach loop can be used to access all values.

<?php
foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}
?>

Associative Arrays in PHP

Associative arrays use named keys to store values instead of numbers. This makes data more understandable, especially when each value has a meaning.

Creating an Associative Array

<?php
$user = [
    "name" => "Amit",
    "age" => 22,
    "city" => "Delhi"
];
?>

Each value connects to a specific key.

Accessing Associative Array Values

The key name is used to access a value.

<?php
echo $user["name"];
?>

This will display Amit.

Looping Through Associative Arrays

<?php
foreach ($user as $key => $value) {
    echo $key . ": " . $value . "<br>";
}
?>

Multidimensional Arrays in PHP

Multidimensional arrays store one or more arrays inside another array. They are used to handle complex data such as student information or product information.

Creating a Multidimensional Array

<?php
$students = [
    ["Amit", 20, "PHP"],
    ["Neha", 21, "Java"]
];
?>

Each inner array represents one student.

Accessing Multidimensional Array Values

<?php
echo $students[0][0];
?>

This will display Amit.

Looping Through Multidimensional Arrays

<?php
foreach ($students as $student) {
    foreach ($student as $detail) {
        echo $detail . " ";
    }
    echo "<br>";
}
?>

Summary

Indexed arrays are used for simple lists. Associative arrays are used to label data. Multidimensional arrays are used to handle complex data. All these arrays are the backbone of PHP data handling.

Link to PHP Control Structures
Link to PHP Operators
Link to PHP Variables and Data Types

You may also like...

1 Response

  1. December 24, 2025

    […] PHP Array TypesPHP Loops in DepthPHP Control Structures […]

Leave a Reply

Your email address will not be published. Required fields are marked *