วันจันทร์ที่ 26 ตุลาคม พ.ศ. 2558

PHP - Indexed arrays

Indexed arrays

$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

แสดงผล I like Volvo, BMW and Toyota.

$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota"; 
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

แสดงผล I like Volvo, BMW and Toyota.

$cars = array("Volvo", "BMW", "Toyota");
$cars[3] = "Mazda";
$cars[4] = "Mitsubishi";
array_push($cars, "Ford", "Benz");

$arrlength = count($cars);
echo "จำนวนรถ = $arrlength <br>";

for ($x = 0; $x < $arrlength; $x++) {
      echo $cars[$x];
      echo "<br>";
}
print_r($cars);

แสดงผล

จำนวนรถ = 7
Volvo
BMW
Toyota
Mazda
Mitsubishi
Ford
Benz
Array ( [0] => Volvo [1] => BMW [2] => Toyota [3] => Mazda [4] => Mitsubishi [5] => Ford [6] => Benz ) 

ไม่มีความคิดเห็น:

แสดงความคิดเห็น