PHP Cheat: Array

// Array Exploring

// menampilkan nilai array
$data = array(“ID” => array(“nama” => “Indonesia”, “uang”=>”RP”),
“MY” => array(“nama” => “Malaysia”, “uang” => “Ringgit, gw ga tau lambangnya”));
print_r($data);
var_dump($data);

// memproses nilai array
$index = array(“Jakarta”, “Kopaja”, “Angkot”, “Maceeeet”);
foreach($index as $i){
echo “$i | “;
}
// Jakarta | Kopaja | Angkot | Maceeeet |

//untuk array dalam array
$assocArr = array(“Jakarta”=>”Barat”, “Tanggerang”=>”Serpong”);
foreach($assocArr as $key=>$value){
echo “$key: $value<br>”;
}//Jakarta: Barat
//Tanggerang: Serpong

// Nested Array Processing
function arrTranverse($arr) {
if(!is_array($arr)) {// cek inputan array
die(“Input bukan Array”);
}
foreach ($arr as $value) {// iterasi di array
if(is_array($value)) {// jika nested array
arrTranverse($value);
} else {
echo strtoupper($value).”<br>”;
}
}
}
$nesArr = array(“Jakarta Barat”,
array(“Tanggerang”, array(“BSD”, “Serpong”)));
arrTranverse($nesArr);

// hitung jumlah array
$arr = array(“satu”, “2”, “3”, “last”);
echo count($arr); // 4

// Convert string ke Array
$str = “aa 1 cc 2 ddd”;
print_r(explode(” “, $str));
// Array ( [0] => aa [1] => 1 [2] => cc [3] => 2 [4] => ddd )

// Convert Array ke String
$arr = array(“aku”, “dia”, “kamu”);
echo implode(” dan “, $arr);
//aku dan dia dan kamu

// menukar array dengan sarang
$arr = array(“a”=>”1”, “b”=>”2”);
print_r(array_flip($arr));
// Array ( [1] => a [2] => b )

// + – element array
$arr = array(“cumi”, “hiu”);
array_push($arr, “teri”);
print_r($arr);//Array ( [0] => cumi [1] => hiu [2] => teri )
array_shift($arr);
print_r($arr); // Array ( [0] => hiu [1] => teri )
array_unshift($arr, “badut”);
print_r($arr); // Array ( [0] => badut [1] => hiu [2] => teri )
array_pop($arr);
print_r($arr); // Array ( [0] => badut [1] => hiu )

// +- array di tengah
$arr = array(“1″,”2″,”3″,”4”);
array_splice($arr, 1,2);
print_r($arr); // Array ( [0] => 1 [1] => 4 )
array_splice($arr, 4, 0, array(“lima”, “6”));
print_r($arr); // Array ( [0] => 1 [1] => 4 [2] => lima [3] => 6 )

// membagi array ke segmen-segmen
$arr = array(“1”, “2”, “3”, “4”, “5”);
$slc = array_slice($arr, 2,4);
print_r($slc); // Array ( [0] => 3 [1] => 4 [2] => 5 )

// menghilangkan nilai double pada array
$arr = array(“1″,”1″,”a”,”a”,”b”);
echo join(“, “, array_unique($arr)); //1, a, b

// menghilangkan gap pada nilai array yg dibuang
$arr = array(0=>”1″, 1=>”2″, 2=>”3″);
unset($arr[1]);
print_r($arr); // Array ( [0] => 1 [2] => 3 )
$nwr = array_values($arr);
]}print_r($nwr); //Array ( [0] => 1 [1] => 3 )

// Nilai acak array
$num = range(1,5);
shuffle($num);
echo join(“, “, $num); // 2, 1, 4, 3, 5 (ex)

//Nilai acak dari beberapa
$num = range(1,12);
$rnd = array_rand($num, 5);
echo join(“, “, $rnd); // 11, 0, 5, 7, 3 (ex)

// membalik nilai array
$arr = array(“1”, “2”, “3”);
print_r(array_reverse($arr)); // Array ( [0] => 3 [1] => 2 [2] => 1 )

// mencari nilai array
$data = array(
“JKT” => “Jakarta”,
“TNG” => “Tanggerang”,
“DPK” => “Depok”);
echo array_key_exists(“JKT”, $data) ? ” Exists” : ” Not”;
echo in_array(“Jakarta”, $data) ? ” Value” : ” Not”;
// Exist Value

// fungsi mencari nilai array
function arrSearch($src, $arr) {
if(!is_array($arr)){die(“bukan Array”);}
foreach($arr as $key=>$value){
if(preg_match(“/$src/i”, $value) || preg_match(“/$src/i”, $key)){
return true; break;
}
}
}
echo arrSearch(“DPK”,$data)? “ada”:”tidak”; // ada
echo arrSearch(“xyz”,$data)? “ada”:”tidak”; // tidak

//Memfilter Array
function positip($val){
return($val>=0) ? true:false;
}
$arr = array(1, -1, 2, -10, 0, -2);
print_r(array_filter($arr, ‘positip’));
//Array ( [0] => 1 [2] => 2 [4] => 0 )

// sorting array
$arr = array(“a”, “d”, “c”, “f”);
sort($arr);
print_r($arr);
//Array ( [0] => a [1] => c [2] => d [3] => f )

// sort pada array bersarang /index
$arr = array(“w”=>”r”,”b”=>”x”, “a”=>”z”);
asort($arr);
print_r($arr);//Array ( [w] => r [b] => x [a] => z )
ksort($arr);
print_r($arr);//Array ( [a] => z [b] => x [w] => r )

// sorting array multidimensi
$data = array();
$data[0] = array(“title”=>”Net Force”, “author”=>”Clan, Tom”, “rating”=>4);
$data[1] = array(“title”=>”DOS”, “author”=>”Zero, Log”, “rating”=>5);
$data[2] = array(“title”=>”PHP”, “author”=>”Indra, iam”, “rating”=>3);
$data[3] = array(“title”=>”WWW”, “author”=>”WWW”, “rating”=>1);
$data[4] = array(“title”=>”Blowfish”, “author”=>”G3n1k”, “rating”=>2);
//memisahkan elemen dalam key yg sama ke masing2 array
foreach ($data as $key=>$value) {
$author[$key] = $value[‘author’];
$title[$key] = $value[‘title’];
$rating[$key] = $value[‘rating’];
}
array_multisort($rating, $author, $title);
print_r($data);
//Array ( [0] => Array ( [title] => WWW [author] => WWW [rating] => 1 ) [1] => Array ( [title] => Blowfish [author] => G3n1k [rating] => 2 ) [2] => Array ( [title] => PHP [author] => Indra, iam [rating] => 3 ) [3] => Array ( [title] => Net Force [author] => Clan, Tom [rating] => 4 ) [4] => Array ( [title] => DOS [author] => Zero, Log [rating] => 5 ) )

// Array Exploring

// menampilkan nilai array
$data = array(“ID” => array(“nama” => “Indonesia”, “uang”=>”RP”),
“MY” => array(“nama” => “Malaysia”, “uang” => “Ringgit, gw ga tau lambangnya”));
print_r($data);
var_dump($data);

// memproses nilai array
$index = array(“Jakarta”, “Kopaja”, “Angkot”, “Maceeeet”);
foreach($index as $i){
echo “$i | “;
}
// Jakarta | Kopaja | Angkot | Maceeeet |

//untuk array dalam array
$assocArr = array(“Jakarta”=>”Barat”, “Tanggerang”=>”Serpong”);
foreach($assocArr as $key=>$value){
echo “$key: $value<br>”;
}//Jakarta: Barat
//Tanggerang: Serpong

// Nested Array Processing
function arrTranverse($arr) {
if(!is_array($arr)) {// cek inputan array
die(“Input bukan Array”);
}
foreach ($arr as $value) {// iterasi di array
if(is_array($value)) {// jika nested array
arrTranverse($value);
} else {
echo strtoupper($value).”<br>”;
}
}
}
$nesArr = array(“Jakarta Barat”,
array(“Tanggerang”, array(“BSD”, “Serpong”)));
arrTranverse($nesArr);

// hitung jumlah array
$arr = array(“satu”, “2”, “3”, “last”);
echo count($arr); // 4

// Convert string ke Array
$str = “aa 1 cc 2 ddd”;
print_r(explode(” “, $str));
// Array ( [0] => aa [1] => 1 [2] => cc [3] => 2 [4] => ddd )

// Convert Array ke String
$arr = array(“aku”, “dia”, “kamu”);
echo implode(” dan “, $arr);
//aku dan dia dan kamu

// menukar array dengan sarang
$arr = array(“a”=>”1”, “b”=>”2”);
print_r(array_flip($arr));
// Array ( [1] => a [2] => b )

// + – element array
$arr = array(“cumi”, “hiu”);
array_push($arr, “teri”);
print_r($arr);//Array ( [0] => cumi [1] => hiu [2] => teri )
array_shift($arr);
print_r($arr); // Array ( [0] => hiu [1] => teri )
array_unshift($arr, “badut”);
print_r($arr); // Array ( [0] => badut [1] => hiu [2] => teri )
array_pop($arr);
print_r($arr); // Array ( [0] => badut [1] => hiu )

// +- array di tengah
$arr = array(“1″,”2″,”3″,”4”);
array_splice($arr, 1,2);
print_r($arr); // Array ( [0] => 1 [1] => 4 )
array_splice($arr, 4, 0, array(“lima”, “6”));
print_r($arr); // Array ( [0] => 1 [1] => 4 [2] => lima [3] => 6 )

// membagi array ke segmen-segmen
$arr = array(“1”, “2”, “3”, “4”, “5”);
$slc = array_slice($arr, 2,4);
print_r($slc); // Array ( [0] => 3 [1] => 4 [2] => 5 )

// menghilangkan nilai double pada array
$arr = array(“1″,”1″,”a”,”a”,”b”);
echo join(“, “, array_unique($arr)); //1, a, b

// menghilangkan gap pada nilai array yg dibuang
$arr = array(0=>”1″, 1=>”2″, 2=>”3”);
unset($arr[1]);
print_r($arr); // Array ( [0] => 1 [2] => 3 )
$nwr = array_values($arr);
]}print_r($nwr); //Array ( [0] => 1 [1] => 3 )

// Nilai acak array
$num = range(1,5);
shuffle($num);
echo join(“, “, $num); // 2, 1, 4, 3, 5 (ex)

//Nilai acak dari beberapa
$num = range(1,12);
$rnd = array_rand($num, 5);
echo join(“, “, $rnd); // 11, 0, 5, 7, 3 (ex)

// membalik nilai array
$arr = array(“1”, “2”, “3”);
print_r(array_reverse($arr)); // Array ( [0] => 3 [1] => 2 [2] => 1 )

// mencari nilai array
$data = array(
“JKT” => “Jakarta”,
“TNG” => “Tanggerang”,
“DPK” => “Depok”);
echo array_key_exists(“JKT”, $data) ? ” Exists” : ” Not”;
echo in_array(“Jakarta”, $data) ? ” Value” : ” Not”;
// Exist Value

// fungsi mencari nilai array
function arrSearch($src, $arr) {
if(!is_array($arr)){die(“bukan Array”);}
foreach($arr as $key=>$value){
if(preg_match(“/$src/i”, $value) || preg_match(“/$src/i”, $key)){
return true; break;
}
}
}

echo arrSearch(“DPK”,$data)? “ada”:”tidak”; // ada
echo arrSearch(“xyz”,$data)? “ada”:”tidak”; // tidak

//Memfilter Array
function positip($val){
return($val>=0) ? true:false;
}
$arr = array(1, -1, 2, -10, 0, -2);
print_r(array_filter($arr, ‘positip’));
//Array ( [0] => 1 [2] => 2 [4] => 0 )

// sorting array
$arr = array(“a”, “d”, “c”, “f”);
sort($arr);
print_r($arr);
//Array ( [0] => a [1] => c [2] => d [3] => f )

// sort pada array bersarang /index
$arr = array(“w”=>”r”,”b”=>”x”, “a”=>”z”);
asort($arr);
print_r($arr);//Array ( [w] => r [b] => x [a] => z )
ksort($arr);
print_r($arr);//Array ( [a] => z [b] => x [w] => r )

// sorting array multidimensi
$data = array();
$data[0] = array(“title”=>”Net Force”, “author”=>”Clan, Tom”, “rating”=>4);
$data[1] = array(“title”=>”DOS”, “author”=>”Zero, Log”, “rating”=>5);
$data[2] = array(“title”=>”PHP”, “author”=>”Indra, iam”, “rating”=>3);
$data[3] = array(“title”=>”WWW”, “author”=>”WWW”, “rating”=>1);
$data[4] = array(“title”=>”Blowfish”, “author”=>”G3n1k”, “rating”=>2);
//memisahkan elemen dalam key yg sama ke masing2 array
foreach ($data as $key=>$value) {
$author[$key] = $value[‘author’];
$title[$key] = $value[‘title’];
$rating[$key] = $value[‘rating’];
}
array_multisort($rating, $author, $title);
print_r($data);
//Array ( [0] => Array ( [title] => WWW [author] => WWW [rating] => 1 ) [1] => Array ( [title] => Blowfish [author] => G3n1k [rating] => 2 ) [2] => Array ( [title] => PHP [author] => Indra, iam [rating] => 3 ) [3] => Array ( [title] => Net Force [author] => Clan, Tom [rating] => 4 ) [4] => Array ( [title] => DOS [author] => Zero, Log [rating] => 5 ) )

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s