Skip to main content

เนื้อหาภาษาไทย / Thai content

System guide Content reviewed

Arrays: การสร้างและเข้าถึงข้อมูล

บทความนี้ได้แนะนำ Array ใน PHP ซึ่งเป็นโครงสร้างข้อมูลที่ใช้เก็บข้อมูลหลายรายการในตัวแปรเดียว โดยได้อธิบายถึงการสร้าง Array ทั้งแบบ Indexed (อ้างอิงด้วยตัวเลข)...

Arrays: การสร้างและเข้าถึงข้อมูล

ในภาษา PHP, Array คือโครงสร้างข้อมูลที่สามารถเก็บข้อมูลหลายรายการไว้ในตัวแปรเดียว ทำให้เราสามารถจัดการข้อมูลที่เป็นชุดได้อย่างมีประสิทธิภาพ บทความนี้จะพาคุณไปรู้จักกับการสร้างและเข้าถึงข้อมูลใน Array ตั้งแต่พื้นฐาน

Array คืออะไร?

Array คือชุดของข้อมูลที่จัดเรียงตามลำดับ โดยแต่ละข้อมูลใน Array จะมี index หรือ key ที่ใช้ในการอ้างอิงถึงข้อมูลนั้นๆ เปรียบเสมือนตู้เก็บของที่มีช่องเก็บของหลายช่อง แต่ละช่องมีหมายเลขกำกับ ทำให้เราสามารถระบุได้ว่าจะหยิบของจากช่องไหน

การสร้าง Array

PHP มีหลายวิธีในการสร้าง Array แต่ที่นิยมใช้กันมี 2 แบบหลักๆ คือ Indexed Array และ Associative Array

Indexed Array

Indexed Array คือ Array ที่ข้อมูลแต่ละรายการถูกอ้างอิงด้วยตัวเลข (index) เริ่มต้นจาก 0 เป็นต้นไป

<?php
// วิธีที่ 1: ใช้ array() function
$colors = array("Red", "Green", "Blue");

// วิธีที่ 2: ใช้ short array syntax (PHP 5.4+)
$fruits = ["Apple", "Banana", "Orange"];

print_r($colors); // แสดงผล Array
echo "<br>";
print_r($fruits); // แสดงผล Array
?>

ผลลัพธ์:

Array ( [0] => Red [1] => Green [2] => Blue )
Array ( [0] => Apple [1] => Banana [2] => Orange )

ในตัวอย่างนี้ `$colors` และ `$fruits` คือ Indexed Array ที่เก็บข้อมูลสีและผลไม้ตามลำดับ โดย "Red" มี index เป็น 0, "Green" มี index เป็น 1, และ "Blue" มี index เป็น 2 เช่นเดียวกับ `$fruits`

Associative Array

Associative Array คือ Array ที่ข้อมูลแต่ละรายการถูกอ้างอิงด้วย key ที่เป็น String แทนที่จะเป็นตัวเลข ทำให้เราสามารถกำหนดความหมายให้กับแต่ละข้อมูลได้ชัดเจนยิ่งขึ้น

<?php
$person = array(
  "name" => "John Doe",
  "age" => 30,
  "city" => "New York"
);

print_r($person);
?>

ผลลัพธ์:

Array ( [name] => John Doe [age] => 30 [city] => New York )

ในตัวอย่างนี้ `$person` คือ Associative Array ที่เก็บข้อมูลส่วนตัวของบุคคล โดย "name" เป็น key ที่อ้างอิงถึงชื่อ, "age" เป็น key ที่อ้างอิงถึงอายุ, และ "city" เป็น key ที่อ้างอิงถึงเมือง

การเข้าถึงข้อมูลใน Array

เมื่อเราสร้าง Array แล้ว สิ่งสำคัญคือการรู้วิธีการเข้าถึงข้อมูลที่เก็บไว้ภายใน

การเข้าถึงข้อมูลใน Indexed Array

เราสามารถเข้าถึงข้อมูลใน Indexed Array ได้โดยใช้ index ของข้อมูลนั้นๆ ภายใน square brackets `[]`

<?php
$colors = array("Red", "Green", "Blue");

echo $colors[0]; // เข้าถึงข้อมูลที่ index 0 (Red)
echo "<br>";
echo $colors[1]; // เข้าถึงข้อมูลที่ index 1 (Green)
echo "<br>";
echo $colors[2]; // เข้าถึงข้อมูลที่ index 2 (Blue)
?>

ผลลัพธ์:

Red
Green
Blue

การเข้าถึงข้อมูลใน Associative Array

เราสามารถเข้าถึงข้อมูลใน Associative Array ได้โดยใช้ key ของข้อมูลนั้นๆ ภายใน square brackets `[]`

<?php
$person = array(
  "name" => "John Doe",
  "age" => 30,
  "city" => "New York"
);

echo $person["name"]; // เข้าถึงข้อมูลที่ key "name" (John Doe)
echo "<br>";
echo $person["age"]; // เข้าถึงข้อมูลที่ key "age" (30)
echo "<br>";
echo $person["city"]; // เข้าถึงข้อมูลที่ key "city" (New York)
?>

ผลลัพธ์:

John Doe
30
New York

การใช้ฟังก์ชัน count() เพื่อหาขนาดของ Array

ฟังก์ชัน `count()` เป็นฟังก์ชันที่ใช้ในการนับจำนวนข้อมูลใน Array

<?php
$colors = array("Red", "Green", "Blue");
$num_colors = count($colors); // นับจำนวนข้อมูลใน $colors

echo "Number of colors: " . $num_colors; // แสดงผลจำนวนสี
echo "<br>";

$person = array(
  "name" => "John Doe",
  "age" => 30,
  "city" => "New York"
);
$num_fields = count($person); // นับจำนวนข้อมูลใน $person

echo "Number of fields in person: " . $num_fields; // แสดงผลจำนวน fields
?>

ผลลัพธ์:

Number of colors: 3
Number of fields in person: 3

การแก้ไขข้อมูลใน Array

เราสามารถแก้ไขข้อมูลใน Array ได้โดยการกำหนดค่าใหม่ให้กับ index หรือ key ที่ต้องการ

<?php
// Indexed Array
$colors = array("Red", "Green", "Blue");
$colors[1] = "Yellow"; // เปลี่ยนค่าที่ index 1 จาก "Green" เป็น "Yellow"
print_r($colors);
echo "<br>";

// Associative Array
$person = array(
  "name" => "John Doe",
  "age" => 30,
  "city" => "New York"
);
$person["age"] = 35; // เปลี่ยนค่าที่ key "age" จาก 30 เป็น 35
print_r($person);
?>

ผลลัพธ์:

Array ( [0] => Red [1] => Yellow [2] => Blue )
Array ( [name] => John Doe [age] => 35 [city] => New York )

การเพิ่มข้อมูลใน Array

เราสามารถเพิ่มข้อมูลใหม่ใน Array ได้โดยการกำหนดค่าให้กับ index หรือ key ใหม่

<?php
// Indexed Array
$colors = array("Red", "Green", "Blue");
$colors[] = "Purple"; // เพิ่ม "Purple" ในตำแหน่งสุดท้ายของ Array (index อัตโนมัติ)
print_r($colors);
echo "<br>";

// Associative Array
$person = array(
  "name" => "John Doe",
  "age" => 30,
  "city" => "New York"
);
$person["country"] = "USA"; // เพิ่ม key "country" และ value "USA" ใน Array
print_r($person);
?>

ผลลัพธ์:

Array ( [0] => Red [1] => Green [2] => Blue [3] => Purple )
Array ( [name] => John Doe [age] => 30 [city] => New York [country] => USA )

สรุป

Array เป็นเครื่องมือที่มีประโยชน์อย่างมากในการจัดการข้อมูลที่เป็นชุดใน PHP เราได้เรียนรู้วิธีการสร้าง Array ทั้งแบบ Indexed และ Associative รวมถึงวิธีการเข้าถึง, แก้ไข, และเพิ่มข้อมูลใน Array นอกจากนี้เรายังได้รู้จักกับฟังก์ชัน `count()` ที่ช่วยในการหาขนาดของ Array ในบทเรียนถัดไป เราจะเรียนรู้เกี่ยวกับการวนซ้ำ Array ด้วย `foreach` loop ซึ่งจะช่วยให้เราสามารถเข้าถึงข้อมูลทุกรายการใน Array ได้อย่างง่ายดาย

Office.in.th Custom Service

Want to build or adapt a system from this structure?

Office.in.th designs and develops enterprise web applications, API integrations, and public- and private-sector management systems, with long-term support.

See our software development service