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

PHP - Associative arrays

Associative arrays

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

echo "Peter is " . $age['Peter'] . " years old.";

แสดงผล Peter is 35 years old.

$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

echo "Peter is " . $age['Peter'] . " years old.";

แสดงผล Peter is 35 years old.

        $age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");
        $age["Bill"] = "90";
        $age["Job"] = "91";
        array_push($age, "77", "78");

        $arrlength = count($age);
        echo "จำนวนคน = $arrlength <br>";

        foreach ($age as $x => $x_value) {
            echo "Key=" . $x . ", Value=" . $x_value;
            echo "<br>";
        }

        print_r($age);

แสดงผล
จำนวนคน = 7
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
Key=Bill, Value=90
Key=Job, Value=91
Key=0, Value=77
Key=1, Value=78
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 [Bill] => 90 [Job] => 91 [0] => 77 [1] => 78 ) 

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

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