Array adalah variabel khusus yang kita gunakan untuk menyimpan atau menampung lebih dari satu nilai dalam satu variabel tanpa harus membuat lebih banyak variabel untuk menyimpan nilai tersebut.
Untuk membuat array di PHP, kami menggunakan fungsi array array( )
Secara default, array variabel apa pun dimulai dengan indeks 0. Jadi, kapan pun Anda ingin memanggil nilai pertama dari array, Anda mulai dengan 0 lalu selanjutnya adalah 1. dan seterusnya
Ada berbagai jenis array di PHP. Mereka
- Array Numerik/Terindeks
- Array asosiatif
- Array Multidimensi
Mari kita lihat bagaimana masing-masing bekerja secara lebih rinci
Apa itu Numerical atau Indexed Arrays?
Array numerik adalah jenis array yang dapat menyimpan string, angka, dan objek. Berikut adalah contoh array numerik
<?php // Numeric/ index arrays $cars = array('Mecedes Benz', 'Hilux', 'Highlander', 'Hummer', 'Limozien'); var_dump($cars); ?>Dari kode di atas saya memiliki variabel $cars yang menyimpan array 5 elemen. Kata kunci var_dump($cars)_ di atas akan menunjukkan kepada kita jumlah total elemen yang kita miliki dalam array, nomor indeks dari setiap array, dan juga panjang setiap elemen dalam array
Anda juga dapat memilih untuk menggunakan kata kunci <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>0, tetapi dalam kasus saya, saya lebih suka menggunakan <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>1 karena memberikan penjelasan yang lebih rinci tentang hasil yang kami dapatkan
Anda juga dapat memilih untuk hanya menampilkan satu elemen/item dari sebuah array di browser web dengan melakukan hal ini
<?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>_Kode di atas mengikuti pola yang sama dengan definisi array kita, yang menyatakan bahwa array dihitung dari nol. Kami ingin menampilkan elemen dengan indeks <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>2. Menghitung dari <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>_3, kita dapat melihat bahwa <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>4 berada di bawah indeks <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>2, menunjukkan bahwa <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>4 adalah nomor yang kita cari dan akan ditampilkan ke browser
Apa itu Array Asosiatif?
Array asosiatif adalah jenis array di mana kuncinya memiliki nilainya sendiri. Dalam array asosiatif, kami menggunakan <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>7 dan <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>8
<?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>_9 adalah keterangan deskriptif dari elemen array yang digunakan untuk mengakses nilai array. Dan <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>_8 adalah nilai yang diberikan ke elemen array
Ada situasi di mana Anda tidak boleh menggunakan larik numerik/terindeks, seperti
- Saat Anda ingin menyimpan usia siswa yang berbeda beserta namanya
- Ketika Anda ingin mencatat gaji karyawan Anda
- Ketika Anda ingin menyimpan skor siswa dalam mata pelajaran yang berbeda
dan seterusnya.
Misalkan kita ingin menetapkan usia untuk sekelompok siswa SMA dengan nama mereka
Kita dapat menggunakan metode array Asosiatif untuk menyelesaikannya. Sebagai contoh
<?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>Kode di atas adalah contoh array asosiatif. <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>7s dari array adalah <?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>2, <?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>3, <?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>4, <?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>5, dan kami menggunakannya untuk menetapkan usia untuk setiap siswa. <?php $numbers = array('8', '20', '40', '58', '88', '200', '400', '500'); var_dump ($numbers [4]); ?>8 dari array adalah <?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>7, <?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>8, <?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>9, dan <?php $student_age = array ( 'Scott_Mcall' => 17, 'Stalenski' => 18, 'Lydia' => 16, 'Allision' => 17, ); echo $student_age ['Scott_Mcall']; //this code will display the age of Scot_Mcall as 17 echo $student_age ['Stalenski']; //this code will display the age of stalenski as 18 echo $student_age ['Lydia']; //this code will display the age of Lydia as 16 echo $student_age ['Allision']; //this code will display the age of Allision as 17 ?>7
Apa itu Array Multidimensi?
Anda dapat menganggap array multidimensi sebagai array dari array. Ini berarti bahwa setiap elemen dalam array memiliki sub-array di dalamnya. Secara umum, array multidimensi memungkinkan Anda menyimpan banyak array dalam satu variabel
Misalkan kita ingin menyimpan Nama, Nomor Pendaftaran, dan Email dari beberapa staf yang bekerja di perusahaan tertentu. Kita dapat menggunakan array multidimensi untuk mengarsipkan ini
Sebagai contoh
Ingat, array mulai menghitung dari indeks 0. Kode di atas adalah contoh array multidimensi karena berisi lebih dari satu array (array of arrays) dengan satu variabel tunggal <?php $Staffs = [ [ 'Name' => 'Derek Emmanuel', 'Reg_No' => 'FE/30304', 'Email' => 'derekemmanuel@gmail.com' ], [ 'Name' => 'Rubecca Michealson', 'Reg_No' => 'FE/20003', 'Email' => 'rmichealsongmail.com' ], [ 'Name' => 'Frank Castle', 'Reg_No' => 'FE/10002', 'Email' => 'fcastle86@gmail.com' ] ]; echo $Staffs [2] ['Email']; // This displays the email of the last staff which is fcastle86@gmail.com echo $staffs [0] ['Name']; //This displays the Name of the staff in the first array (index 0) which is Derek Emmanuel // you can access the information of any staff you wish to by using echo $(variable name) [index number] ['array element key']. ?>2
<?php $Staffs = [ [ 'Name' => 'Derek Emmanuel', 'Reg_No' => 'FE/30304', 'Email' => 'derekemmanuel@gmail.com' ], [ 'Name' => 'Rubecca Michealson', 'Reg_No' => 'FE/20003', 'Email' => 'rmichealsongmail.com' ], [ 'Name' => 'Frank Castle', 'Reg_No' => 'FE/10002', 'Email' => 'fcastle86@gmail.com' ] ]; echo $Staffs [2] ['Email']; // This displays the email of the last staff which is fcastle86@gmail.com echo $staffs [0] ['Name']; //This displays the Name of the staff in the first array (index 0) which is Derek Emmanuel // you can access the information of any staff you wish to by using echo $(variable name) [index number] ['array element key']. ?>_3 menampilkan email staf yang termasuk dalam indeks <?php $Staffs = [ [ 'Name' => 'Derek Emmanuel', 'Reg_No' => 'FE/30304', 'Email' => 'derekemmanuel@gmail.com' ], [ 'Name' => 'Rubecca Michealson', 'Reg_No' => 'FE/20003', 'Email' => 'rmichealsongmail.com' ], [ 'Name' => 'Frank Castle', 'Reg_No' => 'FE/10002', 'Email' => 'fcastle86@gmail.com' ] ]; echo $Staffs [2] ['Email']; // This displays the email of the last staff which is fcastle86@gmail.com echo $staffs [0] ['Name']; //This displays the Name of the staff in the first array (index 0) which is Derek Emmanuel // you can access the information of any staff you wish to by using echo $(variable name) [index number] ['array element key']. ?>4. Dalam kasus kami ini akan menampilkan <?php $Staffs = [ [ 'Name' => 'Derek Emmanuel', 'Reg_No' => 'FE/30304', 'Email' => 'derekemmanuel@gmail.com' ], [ 'Name' => 'Rubecca Michealson', 'Reg_No' => 'FE/20003', 'Email' => 'rmichealsongmail.com' ], [ 'Name' => 'Frank Castle', 'Reg_No' => 'FE/10002', 'Email' => 'fcastle86@gmail.com' ] ]; echo $Staffs [2] ['Email']; // This displays the email of the last staff which is fcastle86@gmail.com echo $staffs [0] ['Name']; //This displays the Name of the staff in the first array (index 0) which is Derek Emmanuel // you can access the information of any staff you wish to by using echo $(variable name) [index number] ['array element key']. ?>5
Jika saya ingin mengakses Email staf di larik pertama, kami akan melakukan hal berikut
<?php $Staffs = [ [ 'Name' => 'Derek Emmanuel', 'Reg_No' => 'FE/30304', 'Email' => 'derekemmanuel@gmail.com' ], [ 'Name' => 'Rubecca Michealson', 'Reg_No' => 'FE/20003', 'Email' => 'rmichealsongmail.com' ], [ 'Name' => 'Frank Castle', 'Reg_No' => 'FE/10002', 'Email' => 'fcastle86@gmail.com' ] ]; echo $Staffs [2] ['Email']; // This displays the email of the last staff which is fcastle86@gmail.com echo $staffs [0] ['Name']; //This displays the Name of the staff in the first array (index 0) which is Derek Emmanuel // you can access the information of any staff you wish to by using echo $(variable name) [index number] ['array element key']. ?>6
Dengan menggunakan metode di atas, Anda dapat mengakses dan menampilkan informasi apapun dalam array dari kode di atas
Kesimpulan
Pada titik ini Anda harus dapat menggunakan tiga jenis array yang berbeda saat mengerjakan proyek PHP
Terima kasih telah membaca
Bersenang-senang membuat kode
IKLAN
IKLAN
IKLAN
IKLAN
Halo, saya menggunakan alias "Derek". Saya mahir dalam berbagai keterampilan teknis, yang saya peroleh dan terus saya asah melalui pendidikan mandiri
Jika Anda membaca sejauh ini, tweet ke penulis untuk menunjukkan bahwa Anda peduli. Tweet terima kasih
Belajar kode secara gratis. Kurikulum open source freeCodeCamp telah membantu lebih dari 40.000 orang mendapatkan pekerjaan sebagai pengembang. Memulai