0% found this document useful (0 votes)
54 views

M6 Array

1. Arrays are a collection of elements of the same type that are stored and accessed using an index. Arrays allow grouping of related data and can be one-dimensional or multi-dimensional. 2. Array indexes can be numeric or associative. Multidimensional arrays have multiple indexes to access elements. 3. The document provides an example of a one-dimensional numeric indexed array in PHP that stores string elements and loops through them to display each value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

M6 Array

1. Arrays are a collection of elements of the same type that are stored and accessed using an index. Arrays allow grouping of related data and can be one-dimensional or multi-dimensional. 2. Array indexes can be numeric or associative. Multidimensional arrays have multiple indexes to access elements. 3. The document provides an example of a one-dimensional numeric indexed array in PHP that stores string elements and loops through them to display each value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

i

as
rm
fo
In
em

i
st

as
Si

rm
5

b.

fo
ARRAY

La

In
em
st
Si
i
as
OBJEKTIF

b.

fo
m

La

In
or
1. Memahami konsep dan pembuatan array.

em
In
2. Memahami indeks numeric dan indeks assosiatif pada array.

em

st
Si
i
3. Memahami Multidimensional Array.
st

as

b.
Si

La
or
b.
La

f
In
Array adalah sekumpulan tempat penyimpanan data yang bertipe

em
dan bernama sama dan memiliki index. Singkatnya, array dapat

i
st

as
Si
diibaratkan sebagai sekumpulan variable yang bertipe sama dan bernama

b
m
i
as

La
or
b.
rm

sama. Untuk membedakan variable-variable tersebut menggunakan index.


La

f
In
fo

em
In

4.1. Bentuk Umum Array

i
m

st

as
te

Si

m
i

4.1.1. Array Satu Dimensi


as
is

or
b.
S

La

f
Array satu dimensi adalah array yang memiliki 1 index.

In
or
b.
La

em
In

Bentuk Umum
m

st
$nama_variabel=array(isi_array1,isi_array2….isi_arrayn);
e

Si
i
st

as

b.
Si

rm
i
as

La
b.

fo
La

In

Contoh 1
m
te

<?php
i
as
is

$angka=array("satu","dua","tiga","empat","li
S

m
i
as

ma");
or
b.
rm

for($n=0;$n<5;$n++)
La

f
In
fo

echo $n+1,"=",$angka[$n],"<br>";
m
In

?>
te
m

is
te

.S
i
as
is

Output 1
b
rm

La
fo

1=satu
In

2=dua
m
te

3=tiga
as
is
S

rm

4=empat
b.

fo
La

In

5=lima
m
te
is
b .S
La
i
as
rm
fo
In
em

i
st

as
Si

rm
b.

fo
Contoh 2

La

In
<?php

em
$date=date('l');

st
$hari["Sunday"]="Minggu";

Si
i
as
$hari["Monday"]="Senin";

b.

fo
m

La
$hari["Tuesday"]="Selasa";

In
or
$hari["Wenesday"]="Rabu";

em
In
$hari["Thursday"]="Kamis";

em

st
$hari["Friday"]="Jumat";

Si
i
st

as
$hari["Saturday"]="Sabtu";

b.
Si

m
echo $date,"=",$hari[$date];

La
or
b.

?>
La

f
In
em

i
st

as
Output :
Si

b
m
i
as

La
or
b.

Monday=Senin
rm

La

f
In
fo

em
In

i
m

4.1.2. Array Multi Dimensi st

as
te

Si

m
i
as

Array multi dimensi adalah array yang memiliki lebih dari satu
is

or
b.
S

La

f
In
index. Array multi dimensi juga dapat dikatakan sebagai array dari array
or
b.
La

em
In

(sekumpulan dari array).


m

st
e

Si
i
st

as

b.
Si

rm
i

Contoh
as

La
b.

fo
La

In
m
te

i
as
Sis

m
i
as

or
b.
rm

La

f
In
fo

m
In

te
m

is
te

.S
i
as
is

b
rm

La
fo
In
m
te

i
as
is
S

rm
b.

fo
La

In
m
te
is
b .S
La
i
as
rm
fo
In
em

i
st

as
Si

rm
b.

fo
<?php

La

In
$peserta=array(

em
"PHP" =>array("Anto","Edi","Sita"),

st
"Delphi" =>array("Anto","Rina"),

Si
i
"Mysql" =>array("Nana","Dito","Ika"),

as

b.

fo
"Java" =>array("Imam")

La

In
or
);

em
In
em
while(list($index1,$nilai1)=each($peserta))

st
Si
{

i
st

as

b.
Si
echo "Peserta Index $index1 :<br>";

La
$nomor=1;

or
b.
La

f
while(list($index2,$nilai2)=each($nilai1))

In
{
echo "$nomor. $nilai2 <br>";
em

i
st

as
$nomor++;
Si

b
m
i
as

La
}

or
b.
rm

}
La

f
In
fo

?>

em
In

i
m

st

as
te

Si

m
i
as
is

or
b.
S

Output :
m

La

f
In
or
b.
La

em
In

Peserta Index PHP :


m

st
e

Si
i

1. Anto
st

as

b.
Si

rm
i
as

2. Edi
La
b.

fo
La

3. Sita
In
m

Peserta Index Delphi :


te

i
as
is

1. Anto
S

m
i
as

or
b.

2. Rina
rm

La

f
In
fo

Peserta Index Mysql :


m
In

te
m

1. Nana
is
te

.S
i
as
is

2. Dito
b
rm

La

3. Ika
fo
In

Peserta Index Java :


m
te

1. Imam
i
as
is
S

rm
b.

fo
La

In
m
te
is
b .S
La
i
as
rm
fo
In
em

i
st

as
Si

rm
b.

fo
Tuntunan Latihan

La

In
1. Aktifkan Start Running Apache pada XAMPP.

em
st
2. Buka Notepad

Si
i
as
3. Ketikkan program pada lembar kerja Notepad

b.

fo
m

La

In
or
4. Simpan di folder XAMPP\htdocs\namafile.php

em
In
5. Buka browser dan ketik localhost\namafile.php pada address bar untuk

em

st
Si
menjalankan program.

i
st

as

b.
Si

La
or
b.
La

f
Latihan

In
em
1. Buatlah file pada htdocs dengan nama latihan4.php

i
st

as
a. Tuliskan script dibawah ini:
Si

b
m
i
as

La
or
b.

<?php
rm

La

f
In
fo

$hargaBaju = array(

em
In

"T-Shirt (Baju Kaos)" => 20000000,

i
m

st

as
te

Si

m
i

"Pullover" => 10000000,


as
is

or
b.
S

La

f
"Baju Polo" => 5000000

In
or
b.
La

em
In

);
m

st
e

Si
i
st

as

b.
Si

echo "Harga T-Shirt Rp.". $hargaBaju ['T-Shirt


rm
i
as

La
b.

fo

(Baju Kaos)']. "<br />";


La

In

echo "Harga Pullover Rp.". $hargaBaju


m
te

i
as

['Pullover']. "<br />";


S is

m
i
as

echo "Harga Baju Polo Rp.". $hargaBaju ['Baju


or
b.
rm

La

f
In

Polo']. "<br />";


fo

m
In

$hargaBaju ['T-Shirt (Baju Kaos)']= "high";


te
m

is
te

$hargaBaju ['Pullover']= "medium";


.S
i
as
is

b
rm

$hargaBaju ['Baju Polo']= "low";


La
fo
In
m

echo "Harga T-Shirt <strong>". $hargaBaju


te

i
as
is

['Pullover']. "</strong><br />";


S

rm
b.

fo
La

In
m
te
is
b .S
La
i
as
rm
fo
In
em

i
st

as
Si

rm
b.
echo "Harga Pullover <strong>". $hargaBaju

fo
La

In
['Pullover']. "</strong><br />";

em
echo "Harga Baju Polo <strong>". $hargaBaju ['Baju

st
Si
i
as
Polo']. "</strong><br />";

b.

fo
m

La
?>

In
or
f

em
In
em

st
Si
Output:

i
st

as

b.
Si

La
or
b.
La

f
In
em

i
st

as
Si

b
m
i
as

La
or
b.
rm

La

f
In
fo

em
In

i
m

st

as
te

Si

m
i
as
is

or
b.
S

La

f
In
or
b.
La

em
In
m

st
e

Si
i
st

as

b.
Si

2. Lengkapilah Program dibawah ini:


rm
i
as

La
b.

fo

<?php
La

In

$nilai = array(
m
te

"mohammad" => array


as
S is

m
i
as

(
or
b.
rm

La

f
In

/Implementasi Sistem Informasi" => 67,


fo

m
In

"Arsitektur dan Organisasi Sistem Komputer"


te
m

is
te

=> 80,
.S
i
as
is

"Pemrograman Berbasis WEB**" => 75


rm

La
fo

),
In
m

"qadir" => array


te

i
as
is

(
S

rm

"Implementasi Sistem Informasi" => 55,


b.

fo
La

In
m
te
is
b .S
La
i
as
rm
fo
In
em

i
st

as
Si

rm
b.
"Arsitektur dan Organisasi Sistem Komputer"

fo
La

In
=> 75,

em
"Pemrograman Berbasis WEB**" => 60

st
Si
i
as
),

b.

fo
m

La
‘zara" => array

In
or
f

em
In
(

em

st
"Implementasi Sistem Informasi" => 70,

Si
i
st

as

b.
Si
"Arsitektur dan Organisasi Sistem Komputer"

La
or
b.

=> 65,
La

f
In
‘Pemrograman Berbasis WEB**" => 70
em

i
st

as
)
Si

b
m
i
as

La
);

or
b.
rm

La

f
In
echo "<h1>NILAI UJIAN TENGAN SEMESTER</h1>";
fo

em
In

echo "<strong>Implementasi Sistem Informasi

i
m

st

as
te

Si
</strong><br>";

m
i
as
is

or
b.
S

echo "mohammad :&nbsp;" ;


La

f
In
or
b.
La

em
echo $nilai['mohammad']['Implementasi Sistem
In
m

st
Informasi'] . "<br />";
e

Si
i
st

as

echo "qadir :&nbsp;" ;


b.
Si

rm
i
as

La
b.

echo $nilai['qadir']['Implementasi Sistem


fo
La

In

Informasi'] . "<br />";


m
te

echo "zara :&nbsp;" ;


i
as
S is

m
i
as

echo $nilai['zara']['Implementasi Sistem


or
b.
rm

La

Informasi'] . "<br />"


In
fo

m
In

echo "<br>";
te
m

is

echo "<strong>Arsitektur dan Organisasi Sistem


te

.S
i
as
is

Komputer </strong><br>";
b
rm

La
fo

echo "mohammad :&nbsp;" ;


In

echo $nilai['mohammad']['Arsitektur dan Organisasi


m
te

i
as

Sistem Komputer'] . "<br />";


is
S

rm

echo "qadir :&nbsp;" ;


b.

fo
La

In
m
te
is
b .S
La
i
as
rm
fo
In
em

i
st

as
Si

rm
b.
echo $nilai['qadir']['Arsitektur dan Organisasi

fo
La

In
Sistem Komputer'] . "<br />";

em
echo "zara :&nbsp;"

st
Si
i
as
echo $nilai['zara']['Arsitektur dan Organisasi

b.

fo
m

La
Sistem Komputer'] . "<br />";

In
or
f

em
In
echo "<br";

em

st
echo "<strong>Pemrograman Berbasis WEB**

Si
i
st

as

b.
Si
</strong><br>";

La
or
b.

echo "mohammad :&nbsp;"


La

f
In
echo $nilai['mohammad']['Pemrograman Berbasis
em

i
st

as
WEB**'] . "<br />";
Si

b
m
i
as

La
echo "qadir :&nbsp;" ;

or
b.
rm

La

f
In
echo $nilai['qadir']['Pemrograman Berbasis WEB**']
fo

em
In

. "<br />";

i
m

st

as
te

Si
echo "zara :&nbsp; ;

m
i
as
is

or
b.
S

echo $nilai['zara]['Pemrograman Berbasis WEB**'] .


La

f
In
or
b.
La

em
"<br />";
In
m

st
e

Si
i
st

as

?>
b.
Si

rm
i
as

La
b.

fo
La

In

Output:
m
te

i
as
S is

m
i
as

or
b.
rm

La

f
In
fo

m
In

te
m

is
te

.S
i
as
is

b
rm

La
fo
In
m
te

i
as
is
S

rm
b.

fo
La

In
m
te
is
b .S
La

You might also like