dev.rean.me
php

PHP Array and Data Types with Examples & Video Demo

Master PHP array types with step-by-step Khmer video tutorials and practical code examples. Learn Indexed Arrays, Associative Arrays, and Multidimensional Arrays in PHP.

2026-08-18 ยท 4 min read

Share:

Hi my friends! Today let me show you 3 Types of Arrays in PHP with clear simple explanations, code examples, and video in Khmer language.

In PHP, we have 3 main types of array:

  1. Indexed Arrays (use number index 0, 1, 2...)
  2. Associative Arrays (use named key => value pair)
  3. Multidimensional Arrays (array inside another array)

Let's learn each type together!

1. Indexed Arrays in PHP

Indexed array is the simplest array type. It automatically assign numbers (index) starting from 0 to store and access each item. Read the official PHP Array Documentation for full details. Before diving deep, review Basic PHP Variables and Data Types if needed!

Example 1: Store fruits list

<?php
$fruits = ["mango", "banana", "watermelon"];

echo $fruits[0]; // Output: mango
echo $fruits[1]; // Output: banana
?>

Example 2: Create empty array and add items

<?php
$colors = [];
$colors[] = "red";
$colors[] = "green";
$colors[] = "blue";

echo "First color is: " . $colors[0]; // Output: red
?>

๐Ÿ’ก Tip: Remember in programming, array index ALWAYS start at number 0, NOT number 1! So $fruits[0] is the first item!

Watch video about Index Arrays in PHP Khmer:

2. Associative Arrays in PHP

Associative array is bit different from indexed array. Instead of using number index, you use custom text name as key (key => value pair) to store data!

Example 1: Store person information

<?php
$person = [
    "name" => "Sok", 
    "gender" => "male", 
    "age" => 30
];

echo "Name: " . $person["name"] . "<br>";   // Output: Sok
echo "Age: " . $person["age"] . " years";    // Output: 30 years
?>

Example 2: Store student exam score

<?php
$scores = [
    "Math" => 95,
    "English" => 88,
    "PHP" => 100
];

echo "PHP Score: " . $scores["PHP"]; // Output: 100
?>

๐Ÿ’ก Tip: Always put quotes " " around key name when accessing value like $person["name"]. If you write $person[name] without quotes, PHP will throw notice warning!

Watch video about Associative Arrays in PHP Khmer:

3. Multidimensional Arrays in PHP

Multidimensional array is array inside another array (nested array). Very useful when you want store table data or multiple database record rows!

Example 1: List of contact users

<?php
$contacts = [
    ["name" => "Raksmey", "phone" => "012345678"],
    ["name" => "Sophal", "phone" => "098765432"]
];

// Get first user name and phone
echo "First user: " . $contacts[0]["name"] . " - Phone: " . $contacts[0]["phone"] . "<br>";
// Get second user name
echo "Second user: " . $contacts[1]["name"];
?>

Example 2: 2D Matrix grid array

<?php
$matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

echo $matrix[0][0]; // Output: 1 (row 0, col 0)
echo $matrix[1][2]; // Output: 6 (row 1, col 2)
?>

๐Ÿ’ก Tip: Use two square brackets [][] to access value in 2D multidimensional array! First [0] select row array, second ["key"] select value inside that array.

Watch video about Multidimensional Arrays in PHP Khmer:

๐Ÿ’ก Next Lesson: Learn how to loop through arrays using PHP Control Structures & Loops, or learn to write reusable code with PHP Functions!

Hope you enjoy learning PHP arrays with me! Don't forget to share this post with your friends. Happy coding! Sharing is caring!

โ† Back to php
Share: