SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
[Tips] Operasional Array PHP

Array merupakan variabel penting dalam semua pemrograman termasuk PHP, dimana array memiliki
kemampuan membawa dalam satu variable dapat memuat banyak value dan key yang serupa. Contoh
Array adalah misal akan kita bentuk sebuah variabel data nama anak-anak kelas III dalam sebuah
sekolah akan diletakkan dalam sebuah tabel:

 nis   nama
001    Banu
002    Budi
003    Bayu
004    Bono


Isi dari sebuah tabel itu dapat dimasukkan dalam sebuah variabel array , dimana tiap item array akan
berisi KEY dan VALUE, dan antara item satu dengan yang lain akan dipisahkan dengan tanda koma,
seperti demikian :

$arr = array("001"=>"Banu","002"=>"Budi","003"=>"Bayu","004"=>"Bono");


Cara lain membuat array adalah dengan :

$arr["001"] = "Banu";
$arr["002"] = "Budi";
$arr["003"] = "Bayu";
$arr["004"] = "Bono";


Adapun sebuah array dapat saja tidak memiliki KEY tapi memiliki Value. Misal:

$arr = array("Banu","Budi","Bayu","Bono");


MENAMPILKAN ARRAY dalam PHP
Menampilkan Array dalam PHP dapat dengan menggunakan fungsi perintah php print_r();

print_r($arr);


JENIS ARRAY
Array dapat meliputi tiga hal:

    • Array Numerik / Numeric Array: adalah operasional array dengan penyebutan key nya adalah
       urutan item array tersebut, dimulai dari angka ke 0 sebagai item array pertama. Contoh diatas
       dapat dikatakan bahwa
       $arr[0] = “Banu”
       $arr[1] = “Budi”
       dan seterusnya. Sehingga jika dilakukan echo dengan PHP:

       echo $arr[0];


       Hasilnya adalah “Banu”.

    • Array Asosiatif / Associative Array : adalah operasional array dengan penyebutan keynya adalah
       nama dari key tersebut. Contoh diatas dapat dikatakan bahwa

       $arr["001"] = “Banu”
       $arr["002"] = “Budi”
       dan seterusnya. Sehingga jika dilakukan echo dengan PHP:

       echo $arr["002"];


       hasilnya adalah “Budi”

    • Array Multidimensional / Multidimensional array : Adalah adanya array di dalam array. Misal :


       $makanan = array(
       "buah" => array("Jeruk","Jambu","Alpokat"),
       "sayuran" => array("Bayam","Kenci","Selada","Mikado"),
       "daging" => array("Kambing","Ayam","Sapi"),
       "nasi" => array("merah","hitam","putih", "jagung");
       );


PENGURAIAN VALUE ARRAY
Penguraian array dapat dilakukan dengan menggunakan looping untuk penyebutan seluruhnya, atau
melakukan echo/print untuk penyebutan peritem. Contoh adalah :

$arr = array("001"=>"Banu","002"=>"Budi","003"=>"Bayu","004"=>"Bono");


foreach ( $arr as $item ){
echo $item . "<br />";
}


atau kadang kita langsung menyebut per item jika bukan looping yang kita perlukan :

echo $arr["002"];


atau

echo $arr[1];


PENGURAIAN KEY DAN VALUE ARRAY
Suatu ketika kita perlu mengurai Value Array beserta Keynya. Maka dapat digunakan looping seperti
demikian :

$arr = array("001"=>"Banu","002"=>"Budi","003"=>"Bayu","004"=>"Bono");


foreach ( $arr as key => $value ){
echo $key . "=" . $value . "<br />";
}


PENGURAIAN ARRAY LANGSUNG MENJADI VARIABEL BARU
Sebuah array dapat dipecah langsung menjadi variabel-variabel baru misal :

$arr = array("nama"=>"bimosaurus","alamat"=>"wonosobo","usia"=>"200 tahun");


Dapat langsung digunakan :

$arr = array("nama"=>"bimosaurus","alamat"=>"wonosobo","usia"=>"200 tahun");
export( $arr );


Maka akan muncul variabel baru :
$nama = “bimosaurus”;
$alamat = “wonosobo”;
$usia = “200 tahun”;

PEMBENTUKAN ARRAY DENGAN EXPLODE
Sebuah array dapat juga terbentuk dari kata atau kalimat yang dipecah dengan menggunakan explode.
Sebagai contoh adalah :

$kalimat = "sebenarnya saya sudah lama ingin sekali menulis";
$arr = explode(" ", $kalimat); // dipecah berdsar tanda " " / spasi
Hasilnya jika dibongkar dengan array numerik adalah :

echo $arr[0];


Hasilnya adalah “sebenarnya”. Coba juga dilakukan perintah fungsi print_r();
Kebalikan dari explode adalah implode

PENGURAIAN ARRAY dari MySQL
Andaikan tabel di atas, akan disebut dengan tabel siswa. Maka sebuah Query dari MySQL akan seperti
berikut:

$sql = "SELECT * FROM `siswa`";
$query = mysql_query( $sql );


Setelah dilakukan query, maka SQL akan menghasilkan array. Array tersebut akan kita urai dengan
beberapa cara. Fungsi yang digunakan juga bisa mysql_fetch_row, mysql_fetch_array, atau
mysql_fetch_assoc. Sedangkan perintah looping yang digunakan akan sangat lebih mudah
menggunakan while.

while( $r = mysql_fetch_array( $q ) ){
echo $r['nis'] . " = " . $r['nama'] . "<br />";
}
}


Apa perbedaan mysql_fetch_row, mysql_fetch_array, dan mysql_fetch_assoc? Perbedaannya adalah
mysql_fetch_row untuk penyebutan array numerik, mysql_fetch_assoc adalah untuk penyebutan array
assosiatif, dan mysql_fetch_array bisa untuk keduanya.

Sekian, semoga bermanfaat




Wahyu Bimo S.
bimosaurus@gmail.com
http://bimosaurus.com

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Operasional array PHP

  • 1. [Tips] Operasional Array PHP Array merupakan variabel penting dalam semua pemrograman termasuk PHP, dimana array memiliki kemampuan membawa dalam satu variable dapat memuat banyak value dan key yang serupa. Contoh Array adalah misal akan kita bentuk sebuah variabel data nama anak-anak kelas III dalam sebuah sekolah akan diletakkan dalam sebuah tabel: nis nama 001 Banu 002 Budi 003 Bayu 004 Bono Isi dari sebuah tabel itu dapat dimasukkan dalam sebuah variabel array , dimana tiap item array akan berisi KEY dan VALUE, dan antara item satu dengan yang lain akan dipisahkan dengan tanda koma, seperti demikian : $arr = array("001"=>"Banu","002"=>"Budi","003"=>"Bayu","004"=>"Bono"); Cara lain membuat array adalah dengan : $arr["001"] = "Banu"; $arr["002"] = "Budi"; $arr["003"] = "Bayu"; $arr["004"] = "Bono"; Adapun sebuah array dapat saja tidak memiliki KEY tapi memiliki Value. Misal: $arr = array("Banu","Budi","Bayu","Bono"); MENAMPILKAN ARRAY dalam PHP Menampilkan Array dalam PHP dapat dengan menggunakan fungsi perintah php print_r(); print_r($arr); JENIS ARRAY
  • 2. Array dapat meliputi tiga hal: • Array Numerik / Numeric Array: adalah operasional array dengan penyebutan key nya adalah urutan item array tersebut, dimulai dari angka ke 0 sebagai item array pertama. Contoh diatas dapat dikatakan bahwa $arr[0] = “Banu” $arr[1] = “Budi” dan seterusnya. Sehingga jika dilakukan echo dengan PHP: echo $arr[0]; Hasilnya adalah “Banu”. • Array Asosiatif / Associative Array : adalah operasional array dengan penyebutan keynya adalah nama dari key tersebut. Contoh diatas dapat dikatakan bahwa $arr["001"] = “Banu” $arr["002"] = “Budi” dan seterusnya. Sehingga jika dilakukan echo dengan PHP: echo $arr["002"]; hasilnya adalah “Budi” • Array Multidimensional / Multidimensional array : Adalah adanya array di dalam array. Misal : $makanan = array( "buah" => array("Jeruk","Jambu","Alpokat"), "sayuran" => array("Bayam","Kenci","Selada","Mikado"), "daging" => array("Kambing","Ayam","Sapi"), "nasi" => array("merah","hitam","putih", "jagung"); ); PENGURAIAN VALUE ARRAY Penguraian array dapat dilakukan dengan menggunakan looping untuk penyebutan seluruhnya, atau melakukan echo/print untuk penyebutan peritem. Contoh adalah : $arr = array("001"=>"Banu","002"=>"Budi","003"=>"Bayu","004"=>"Bono"); foreach ( $arr as $item ){
  • 3. echo $item . "<br />"; } atau kadang kita langsung menyebut per item jika bukan looping yang kita perlukan : echo $arr["002"]; atau echo $arr[1]; PENGURAIAN KEY DAN VALUE ARRAY Suatu ketika kita perlu mengurai Value Array beserta Keynya. Maka dapat digunakan looping seperti demikian : $arr = array("001"=>"Banu","002"=>"Budi","003"=>"Bayu","004"=>"Bono"); foreach ( $arr as key => $value ){ echo $key . "=" . $value . "<br />"; } PENGURAIAN ARRAY LANGSUNG MENJADI VARIABEL BARU Sebuah array dapat dipecah langsung menjadi variabel-variabel baru misal : $arr = array("nama"=>"bimosaurus","alamat"=>"wonosobo","usia"=>"200 tahun"); Dapat langsung digunakan : $arr = array("nama"=>"bimosaurus","alamat"=>"wonosobo","usia"=>"200 tahun"); export( $arr ); Maka akan muncul variabel baru : $nama = “bimosaurus”; $alamat = “wonosobo”; $usia = “200 tahun”; PEMBENTUKAN ARRAY DENGAN EXPLODE Sebuah array dapat juga terbentuk dari kata atau kalimat yang dipecah dengan menggunakan explode. Sebagai contoh adalah : $kalimat = "sebenarnya saya sudah lama ingin sekali menulis"; $arr = explode(" ", $kalimat); // dipecah berdsar tanda " " / spasi
  • 4. Hasilnya jika dibongkar dengan array numerik adalah : echo $arr[0]; Hasilnya adalah “sebenarnya”. Coba juga dilakukan perintah fungsi print_r(); Kebalikan dari explode adalah implode PENGURAIAN ARRAY dari MySQL Andaikan tabel di atas, akan disebut dengan tabel siswa. Maka sebuah Query dari MySQL akan seperti berikut: $sql = "SELECT * FROM `siswa`"; $query = mysql_query( $sql ); Setelah dilakukan query, maka SQL akan menghasilkan array. Array tersebut akan kita urai dengan beberapa cara. Fungsi yang digunakan juga bisa mysql_fetch_row, mysql_fetch_array, atau mysql_fetch_assoc. Sedangkan perintah looping yang digunakan akan sangat lebih mudah menggunakan while. while( $r = mysql_fetch_array( $q ) ){ echo $r['nis'] . " = " . $r['nama'] . "<br />"; } } Apa perbedaan mysql_fetch_row, mysql_fetch_array, dan mysql_fetch_assoc? Perbedaannya adalah mysql_fetch_row untuk penyebutan array numerik, mysql_fetch_assoc adalah untuk penyebutan array assosiatif, dan mysql_fetch_array bisa untuk keduanya. Sekian, semoga bermanfaat Wahyu Bimo S. bimosaurus@gmail.com http://bimosaurus.com