> For the complete documentation index, see [llms.txt](https://learn.devlabss.my.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.devlabss.my.id/php/bab-4-mengelola-array/4.2-associative-array.md).

# 4.2 Associative Array

Associative array memudahkan Anda memberi nama pada setiap item data.

Struktur ini sangat berguna untuk data profil, pengaturan, atau objek sederhana yang punya atribut jelas.

### Video pengantar

Tonton video ini untuk mendapat gambaran awal sebelum lanjut ke materi inti.

{% embed url="<https://youtu.be/mNgOuUUp1I0?si=DMzIkQHgyeMS8kpQ>" %}

### Tujuan belajar

Setelah mempelajari bagian ini, Anda diharapkan bisa:

* memahami perbedaan indexed array dan associative array
* membuat data dengan pasangan kunci dan nilai
* mengambil isi array dengan nama kunci

### Apa itu associative array

Jika indexed array memakai angka sebagai indeks, associative array memakai nama kunci.

Cara ini lebih mudah dibaca saat data punya arti yang jelas.

Contohnya adalah data siswa seperti nama, kelas, dan jurusan.

### Bentuk dasar associative array

```php
<?php
$siswa = [
    "nama" => "Rina",
    "kelas" => "XI RPL 1",
    "jurusan" => "RPL"
];
?>
```

Pada contoh itu:

* `nama`, `kelas`, dan `jurusan` adalah kunci
* `Rina`, `XI RPL 1`, dan `RPL` adalah nilainya

### Mengakses isi associative array

```php
<?php
$siswa = [
    "nama" => "Rina",
    "kelas" => "XI RPL 1",
    "jurusan" => "RPL"
];

echo $siswa["nama"];
?>
```

Hasilnya:

```
Rina
```

### Menampilkan beberapa nilai

```php
<?php
$siswa = [
    "nama" => "Rina",
    "kelas" => "XI RPL 1",
    "jurusan" => "RPL"
];

echo "Nama: " . $siswa["nama"];
echo "<br>";
echo "Kelas: " . $siswa["kelas"];
echo "<br>";
echo "Jurusan: " . $siswa["jurusan"];
?>
```

### Mengubah nilai

```php
<?php
$siswa = [
    "nama" => "Rina",
    "kelas" => "XI RPL 1"
];

$siswa["kelas"] = "XI RPL 2";

echo $siswa["kelas"];
?>
```

### Menambah pasangan data baru

```php
<?php
$siswa = [
    "nama" => "Rina",
    "kelas" => "XI RPL 1"
];

$siswa["umur"] = 16;

echo $siswa["umur"];
?>
```

### Kapan associative array dipakai

Gunakan associative array saat:

* data punya atribut yang jelas
* Anda ingin nama kunci lebih mudah dibaca
* urutan angka tidak terlalu penting

### Contoh kasus sederhana

```php
<?php
$barang = [
    "nama" => "Mouse",
    "harga" => 75000,
    "stok" => 12
];

echo "Barang: " . $barang["nama"];
echo "<br>";
echo "Harga: " . $barang["harga"];
?>
```

### Kesalahan umum

* memakai kunci yang belum ada
* salah menulis nama kunci
* tertukar antara indeks angka dan kunci teks

### Latihan singkat

Buat associative array untuk data diri Anda yang berisi:

* nama
* kelas
* jurusan
* hobi

Lalu tampilkan semuanya dengan `echo`.

### Poin evaluasi bab 4.2.

Pastikan Anda sudah memahami poin berikut:

* Saya paham associative array memakai kunci dan nilai.
* Saya bisa mengambil nilai dengan nama kunci.
* Saya bisa menambah dan mengubah data pada associative array.
* Saya tahu kapan associative array lebih cocok dipakai.

Jika semua sudah jelas, lanjut ke [4.3 Multidimensional Array](/php/bab-4-mengelola-array/4.3-multidimensional-array.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.devlabss.my.id/php/bab-4-mengelola-array/4.2-associative-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
