<?php
$arr = array("one", "two", "three");reset($arr);
while (list(, $value) = each($arr)) {
echo "Value: $value<br />\n";
}
foreach ($arr as $value) {
echo "Value: $value<br />\n";
}?>
================================
sama juga, tapi ada key nya
<?php
$arr = array("one", "two", "three");reset($arr);
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}?>
========================================
<?php
echo "<br/>cuyyyy 1";
/* foreach example 1: value only */
$a = array(1, 2, 3, 17);
foreach ($a as $v) {
echo "<pre>";
echo "Current value of \$a: $v.\n";
echo "</pre>";
}
echo "<br/>cuyyyy 2";
/* foreach example 2: value (with its manual access notation printed for illustration) */
$a = array(1, 2, 3, 17);
$i = 0; /* for illustrative purposes only */
foreach ($a as $v) {
echo "<pre>";
echo "\$a[$i] => $v.\n";
echo "</pre>";
$i++;
}
echo "<br/>cuyyyy 3";
/* foreach example 3: key and value */
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $k => $v) {
echo "<pre>";
echo "\$a[$k] => $v.\n";
echo "</pre>";
}
echo "<br/>cuyyyy 4";
echo "<br/>";
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
// echo "<pre>";
echo "$v2\n";
// echo "/<pre>";
}
}
echo "<br/>cuyyyy 5";
echo "<br/>";
/* foreach example 5: dynamic arrays */
foreach (array(1, 2, 3, 4, 5) as $v) {
// echo "<pre>";
echo "$v\n";
// echo "</pre>";
}
?>
===================================================
lebih jelasnya liat disini aja deh :)
http://php.net/manual/en/control-structures.foreach.php
Langganan:
Posting Komentar (Atom)
0 Response to "belajar foreach beragam"
Posting Komentar