Array ကို ေႀကျငာနည္းပါ။ simple program ေလးတစ္ပုဒ္ေလာက္ ေရးႀကည့္ရေအာင္။
<?php
$a = array("A","B","C");
echo $a;
var_dump($a);
print_r($a);
echo "<pre>";
var_dump($a);
echo "</pre>";
?>
ဒီ code ေတြကို စက္ထဲမွာ စမ္းႀကည့္ပါ .... Array ကို output ထုတ္သြားတဲ့ ပံုစံအမ်ိဳးမ်ိဳး ေတြ ့ရလိမ့္မယ္။
Array Exercise 1
<html>
<head>
<title>Simple Array - Weather</title>
</head>
<body>
<h2>How's the weather?</h2>
<?php
$weather=array(
"rain",
"sunshine",
"clouds",
"hail",
"sleet",
"snow",
"wind"
);
echo "<p>We've seen all kinds of weather this
month. At the beginning of the month, ";
echo "we had $weather[5] and $weather[6]. Then came
$weather[1] with a few weather[2] ";
echo "and some $weather[0]. At least we didn't get
any $weather[3] or $weather[4].</p>";
?>
</body>
</html> ဒီ program မွာ ကြ်န္ေတာ္တို ့weather ဆုိတဲ့ array တစ္ခုေဆာက္လိုက္တယ္။ ျပီးေတာ့မွ Array ထဲက value ေတြကို echo ဆိုျပီး ျပန္ထုတ္တယ္။ $weather[5] ဆိုတာမွာ snow.. $weather[6] မွာ wind ..။ အဲလိုေလးပါပဲ... လြယ္ပါတယ္။ တစ္ခုရွိတာက array ထဲက value ေတြကို count လုပ္တဲ့အခါမွာ $weather[0] ဆိုတာက စ count လုပ္ရတယ္။ $weather[0] က rain ဆိုတဲ့ value ကို ေခၚလိုက္တာပါ။ ခက္ခက္ခဲခဲေတာ့မရွိပါဘူး။
Array Exercise 2
<html>
<head>
<title>Simple
Array Loop - Cities</title>
</head>
<body>
<h2>Large
Cities<br /></h2>
<?php
$cities=array(
"Tokyo",
"Mexico City",
"New York City",
"Mumbai",
"Seoul",
"Shanghai",
"Lagos",
"Buenos Aires",
"Cairo",
"London"
);
foreach($cities
as $c)
{
echo
"$c, ";
}
//Sort
array.
sort($cities);
//Print
array as bulleted list.
echo
"\n<ul>\n" ;
foreach($cities
as $c)
{
echo
"<li>$c</li>\n";
}
echo
"</ul>" ;
//Add
four cities to array.
array_push($cities,
"Los Angeles", "Calcutta", "Osaka",
"Beijing") ;
//Sort
again, and print bulleted list.
sort($cities);
echo
"\n<ul>\n";
foreach($cities
as $c)
{
echo
"<li>$c</li>\n";
}
echo
"</ul>";
?>
</body>
</html>ပထမဆံုးအေနနဲ့ cities ဆိုတဲ့ array တစ္ခုေဆာက္ထားတယ္.. အဲဒီ ေအာက္က foreach ဆိုျပီး looping ပတ္ထားတာက Array ထဲမွာ ရွိတဲ့ value ေတြကို print လုပ္တာပါ... foreach($cities as $c) အဲဒီမွာ $c ဆိုတာက Array ထဲက value ေတြကို print out လုပ္တဲ့အခါက်ရင္ comma ခံျပီး print လုပ္တာပါ။
ျပီးေတာ့ sort ဆိုတဲ့ method သံုးျပီး array ကို sorting စီတယ္။
No comments:
Post a Comment