Array merupakan sekumpulan variabel yang memiliki tipe data yang sama, untuk membedakannya antara variabel yang satu dengan yang lain di dalam Array dibedakan berdasarkan subscriptnya.
Oke untuk lebih memahaminya berikut beberapa sintax array menggunakan php,
Simple Array
$a = array(
'a' => 'ayam',
'b' => 'bebek',
'c' => 'cacing',
);
//doesn't mater if the last array not use ','
$b = array(
'a' => 'ayam',
'b' => 'bebek',
'c' => 'cacing'
);
//show array
var_dump($a);
echo "<br>";
var_dump($b);
echo "<br><br>";
//show beauty array
foreach ($a as $key => $value) {
echo "$key: $value <br>";
}
echo "<br>";
//show string array
// '\' for write parse html on php
echo "string array \$a => a = ".$a['a'];
echo "<br>";
echo "string array \$b => c = ".$b['c'];
echo "<br><br>";
Sorting Array
$unsort = array(
'1' => 'ayam',
'3' => 'cacing',
'2' => 'bebek',
);
//unsort array
echo "unsort<br>";
foreach ($unsort as $key => $value) {
echo "$key: $value <br>";
}
echo "<br>";
//sorting array asc
function sortArray($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($unsort, "sortArray");
echo "sorted<br>";
foreach ($unsort as $key => $value) {
echo "$key: $value <br>";
}
echo "<br>";
//sorting array desc
usort($unsort, function($a, $b)
{
return $b - $a;
});
echo "sorted with short way<br>";
foreach ($unsort as $key => $value) {
echo "$key: $value <br>";
}
echo "<br>";
//sorting sub array key
$unsorts = array(
'0' => array(
'posisi' => '2',
'content' => 'bebek',
),
'1' => array(
'posisi' => '3',
'content' => 'cacing',
),
'2' => array(
'posisi' => '1',
'content' => 'ayam',
),
);
echo "<br>";
//show array
var_dump($unsorts);
echo "<br><br>";
//show unsort array with nice display
echo "unsort<br>";
for ($i=0; $i < count($unsorts) ; $i++) {
echo $unsorts[$i]['posisi'].' : '.$unsorts[$i]['content'].'<br>';
}
echo "<br>";
//sorting desc array with key date
usort($unsorts, function($a, $b)
{
return $b['posisi'] - $a['posisi'];
});
//show sorted desc array with nice display
echo "sorted desc<br>";
for ($i=0; $i < count($unsorts) ; $i++) {
echo $unsorts[$i]['posisi'].' : '.$unsorts[$i]['content'].'<br>';
}
echo "<br>";
//sorting asc array with key date
usort($unsorts, function($a, $b)
{
return $a['posisi'] - $b['posisi'];
});
//show sorted asc array with nice display
echo "sorted asc<br>";
for ($i=0; $i < count($unsorts) ; $i++) {
echo $unsorts[$i]['posisi'].' : '.$unsorts[$i]['content'].'<br>';
}
echo "<br>";
//array sort, reverse, join
$the_array = array("asd", "sdas", "dsa", "qwerq");
sort($the_array);
print join(",",$the_array);
rsort($the_array);
print join("::", $the_array);
output:
asd,dsa,qwerq,sdas
sdas::qwerq::dsa::asd
//array push
$apalah = array();
array_push($apalah, "hallo");
array_push($apalah, "aloha");
array_push($apalah, "hai");
array_push($apalah, "hoii");
array_push($apalah, "yaaa!");
print count($apalah);
output:
5
Array to json
$array_to_json = array(
'1' => 'ayam',
'3' => 'cacing',
'2' => 'bebek',
);
$json = json_encode($array_to_json);
echo $json;