#30 Fungsi Array dan Contoh Penggunaan Data Array pada PHP yang Wajib Kamu Tau

Posted on

Pada sesi ini, kita akan belajar pemrograman PHP dan mengenal fungsi-fungsi atau metode Array yang bisa digunakan dalam aplikasi PHP. Tentu dengan menggunakan array akan mempermudah kita dalam membuat kelompok data, dan akan menghemat penulisan code program serta penggunaan variabel-variable.

Nah, mungkin masih banyak yang belum tau betapa dahsyatnya fungsi-fungsi Array yang sudah disediakan oleh PHP sendiri yang bisa digunakan untuk mempermudah dalam mengolah data-data yang kompleks, berikut ini metode Array pada PHP lengkap dengan contoh penggunaannya.

is_array()

<?php
// Array
$varr1 = ['kodeajaib','sectorcode'];
// Variable
$varr2 = 'kodeajaib.com';
// cek apakah varr1 array atau tidak
if (is_array($varr1)) {
echo "variable varr1 adalah Array";
} else {
echo "Variable varr1 bukan Array";
}
echo "<br>";
// cek apakah varr2 array atau tidak
if (is_array($varr2)) {
echo "variable varr2 adalah Array";
} else {
echo "Variable varr2 bukan Array";
}
?>

explode()


<?php
// contoh pertama
$nama = "butet buyung ucok keneng upik opung";
$data = explode(" ", $nama);

foreach ($data as $key => $value) {
echo $value;
echo "<br>";
}
// butet
// buyung
// ucok
// keneng
// upik
// opung

// contoh dua
$root = "kodeajaib:sectorcode:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $root);
echo $user;
echo $pass;

// kodeajaib
//sectorcode
?>

implode()

<?php
$myArr = ['kucing','kambing','sapi'];
$data = implode(",", $myArr);
echo $data;
// kucing,kambing,sapi
?>

preg_split()

<?php
$varr = preg_split("/[s,]+/", "Selamat datang di kodeajaib.com");
foreach ($varr as $key => $value) {
echo $value;
echo "<br>";
}
//selamat
//datang
//di
//kodeajaib.com
?>

str_split()

<?php
$myStr = "Selamat datang di KodeAjaib.com";
$arr1 = str_split($myStr);
$arr2 = str_split($myStr, 3);

echo "<pre>";
print_r($arr1);
echo "<br>";
print_r($arr2);
echo "</pre>";

// Array
// (
// [0] => S
// [1] => e
// [2] => l
// [3] => a
// [4] => m
// [5] => a
// [6] => t
// [7] =>
// [8] => d
// [9] => a
// [10] => t
// [11] => a
// [12] => n
// [13] => g
// [14] =>
// [15] => d
// [16] => i
// [17] =>
// [18] => K
// [19] => o
// [20] => d
// [21] => e
// [22] => A
// [23] => j
// [24] => a
// [25] => i
// [26] => b
// [27] => .
// [28] => c
// [29] => o
// [30] => m
// )

// Array
// (
// [0] => Sel
// [1] => ama
// [2] => t d
// [3] => ata
// [4] => ng
// [5] => di
// [6] => Kod
// [7] => eAj
// [8] => aib
// [9] => .co
// [10] => m
// )
?>

unset()

<?php
function foo() {
static $bar;
$bar++;
echo "Before unset: $bar, ";
unset($bar);
$bar = 23;
echo "after unset: $bar <br>";
}

foo();
foo();
foo();

// Before unset: 1, after unset: 23
// Before unset: 2, after unset: 23
// Before unset: 3, after unset: 23
?>

sort()

<?php
$hewan = ['kambing','kucing','ikan','naga','ular','sapi'];
sort($hewan);
foreach ($hewan as $key => $value) {
echo "Hewan[" . $key . "] = " . $value . "<br>";
}
// Hewan[0] = ikan
// Hewan[1] = kambing
// Hewan[2] = kucing
// Hewan[3] = naga
// Hewan[4] = sapi
// Hewan[5] = ular
?>

sizeof()

<?php
$arr = ['hewan' => [
'kucing', 'kambing', 'ayam'],
'bunga' => ['mawar', 'matahari', 'melati']
];

// recursive count
echo sizeof($arr, COUNT_RECURSIVE);
// normal count
echo sizeof($arr);
//8
//2
?>

shuffle()

<?php
$arrNumb = range(1, 20);
shuffle($arrNumb);
foreach ($arrNumb as $value) {
echo $value ." ";
}
// nomor random 16 15 6 11 12 7 3 10 17 18 13 8 5 19 20 14 9 1 2 4
?>

ksort()

<?php
$hewan = array("d"=>"kambing", "a"=>"kucing", "b"=>"ayam", "c"=>"harimau");
ksort($hewan);
foreach ($hewan as $key => $value) {
echo $key = $value. "<br>";
}
// kucing
// ayam
// harimau
// kambing
?>

asort()

<?php
$hewan = array("d"=>"kambing", "a"=>"kucing", "b"=>"ayam", "c"=>"harimau");
asort($hewan);
foreach ($hewan as $key => $value) {
echo $key = $value. "<br>";
}
// ayam
// harimau
// kambing
// kucing
?>

in_array()

<?php
$pemrogramman = ["PHP", ".NET", "Android", "Python"];
if (in_array("Android", $pemrogramman)) {
echo "Pemrogramman Android ditemukan";
}
echo "<br>";
if (in_array("Python", $pemrogramman)) {
echo "Pemrogramman Python juga ditemukan";
}
// Pemrogramman Android ditemukan
// Pemrogramman Python juga ditemukan
?>

extract()

<?php
$size = "large";
$var_array = ["color" => "blue",
"size" => "medium",
"shape" => "sphere"];
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size";
// blue, large, sphere, medium
?>

each()

<?php
$arr1 = ["kucing", "kambing", "musang", "sapi", "kera", "gajah"];
$arr2 = each($arr1);
print_r($arr2);
// Array
// (
// [1] => kucing
// [value] => kucing
// [0] => 0
// [key] => 0
// )
?>

count()

<?php
$arr = ['hewan' => [
'kucing', 'kambing', 'ayam'],
'bunga' => ['mawar', 'matahari', 'melati']
];

// recursive count
echo count($arr, COUNT_RECURSIVE);
// normal count
echo count($arr);
//8
//2
?>

compact()

<?php
$kota = "Palembang";
$provinsi = "Sumatera selatan";
$kegiatan = "Ngoding";
$location_vars = array("kota", "prov");
$result = compact("kegiatan", $location_vars);
print_r($result);
// Array
// (
// [kegiatan] => Ngoding
// [kota] => Palembang
// )
?>

array_unshift()

<?php
$arr1 = array("Kambing", "kucing");
array_unshift($arr1, "kera", "gajah");
print_r($arr1);
// Array
// (
// [0] => kera
// [1] => gajah
// [2] => Kambing
// [3] => kucing
// )
?>

array_unique()

<?php
$arr1 = ["a" => "gajah",
"kucing",
"b" => "gajah",
"singa",
"singa"
];
$hasil = array_unique($arr1);
print_r($hasil);
// Array
// (
// [a] => gajah
// [0] => kucing
// [1] => singa
// )
?>

array_sum()

<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "<br>";

$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b);

// sum(a) = 20
// sum(b) = 6.9
?>

array_splice()

<?php
$arrHewan = ["Naga", "Rusa", "Jerapah", "zebra"];
array_splice($arrHewan, 2);
// $arrHewan sekarang adalah ["Naga", "Rusa")

$arrHewan = ["Naga", "Rusa", "Jerapah", "zebra"];
array_splice($arrHewan, 1, -1);
// $arrHewan sekarang adalah ["Naga", "zebra")

$arrHewan = ["Naga", "Rusa", "Jerapah", "zebra"];
array_splice($arrHewan, 1, count($arrHewan), "Kuda");
// $arrHewan sekarang adalah ["Naga", "Kuda")

$arrHewan = ["Naga", "Rusa", "Jerapah", "zebra"];
array_splice($arrHewan, -1, 1, ["cicak", "kancil"]);
// $arrHewan sekarang adalah ["Naga", "Rusa",
// "Jerapah", "cicak", "kancil")

$arrHewan = ["Naga", "Rusa", "Jerapah", "zebra"];
array_splice($arrHewan, 3, 0, "bangau");

print_r($arrHewan);

// Array
// (
// [0] => Naga
// [1] => Rusa
// [2] => Jerapah
// [3] => bangau
// [4] => zebra
// )
?>

array_slice()

<?php
$arrData = ["php", "php framework", "laravel", "lumen", "CI"];
$data = array_slice($arrData, 2);
$data = array_slice($arrData, -2, 1);
$data = array_slice($arrData, 0, 3);

print_r(array_slice($arrData, 2, -1));
print_r(array_slice($arrData, 2, -1, true));

// Array
// (
// [0] => laravel
// [1] => lumen
// )
// Array
// (
// [2] => laravel
// [3] => lumen
// )
?>

array_shift()

<?php
$arrData = ["Butet", "buyung", "upik", "opung"];
$arrNama = array_shift($arrData);
print_r($arrData);

// Array
// (
// [0] => buyung
// [1] => upik
// [2] => opung
// )
?>

array_reverse()

<?php
$arrData = [
"php",
4.0,
["green","red"]
];
$reversed = array_reverse($arrData);
$preserved = array_reverse($arrData, true);
echo "<pre>";
print_r($arrData);
print_r($reversed);
print_r($preserved);

// Array
// (
// [0] => php
// [1] => 4
// [2] => Array
// (
// [0] => green
// [1] => red
// )
//
// )
// Array
// (
// [0] => Array
// (
// [0] => green
// [1] => red
// )
//
// [1] => 4
// [2] => php
// )
// Array
// (
// [2] => Array
// (
// [0] => green
// [1] => red
// )
//
// [1] => 4
// [0] => php
// )
?>

array_replace()

<?php
$base = ["orange", "banana", "apple", "raspberry"];
$replacements = [0 => "pineapple",
4 => "cherry"];
$replacements2 = [0 => "grape"];

$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);

// Array
// (
// [0] => grape
// [1] => banana
// [2] => apple
// [3] => raspberry
// [4] => cherry
// )
?>

array_rand()

<?php
$arrPemrograman = ["PHP", "LARAVEL", "CI", "YII", "LUMEN"];
$key = array_rand($arrPemrograman, 2);
echo "<pre>";
echo $arrPemrograman[$key[0]] . "<br>";
echo $arrPemrograman[$key[1]] . "<br>";
// random string
// YII
// LUMEN
?>

array_push()


<?php
$arrData = ["PHP", "LARAVEL"];
array_push($arrData, ".NET", "JAVA");
print_r($arrData);

// Array
// (
// [0] => PHP
// [1] => LARAVEL
// [2] => .NET
// [3] => JAVA
// )
?>

array_pop()

<?php
$arrData = ["PHP", "LARAVEL"];
array_pop($arrData);
print_r($arrData);

// Array
// (
// [0] => PHP
// )
?>

array_marge()

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

// Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )
?>

array_chunk()

<?php
$arrData = ['a', 'b', 'c', 'd', 'e'];
print_r(array_chunk($arrData, 2));
print_r(array_chunk($arrData, 2, true));

// Array
// (
// [0] => Array
// (
// [0] => a
// [1] => b
// )
//
// [1] => Array
// (
// [0] => c
// [1] => d
// )
//
// [2] => Array
// (
// [0] => e
// )
//
// )
// Array
// (
// [0] => Array
// (
// [0] => a
// [1] => b
// )
//
// [1] => Array
// (
// [2] => c
// [3] => d
// )
//
// [2] => Array
// (
// [4] => e
// )
//
// )
?>

array_combine()

<?php
$arr1 = ['PHP', 'Delphi', 'ANDROID'];
$arr2 = ['WEB', 'Desktop', 'Mobile'];
$result = array_combine($arr1, $arr2);
print_r($result);

// Array
// (
// [PHP] => WEB
// [Delphi] => Desktop
// [ANDROID] => Mobile
// )
?>

Anda bisa berlangganan melalui email agar tidak ketinggalan tutorial-tutorial tentang pemrograman PHP terbaru update setiap harinya.

See you next lessons and Happy coding …..

Leave a Reply

Your email address will not be published. Required fields are marked *