Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP การเพิ่มค่าเข้า Associative Array

เพื่อเพิ่มค่าเข้า associative array ให้ใช้ brackets [] [] ก่อนอื่นสร้าง associative array -

$details= array ( 
    'id' => '101', 
    'name' => 'John Smith', 
    'countryName' => 'US' 
);

โค้ด PHP สำหรับแทรกค่าดังนี้ -

ตัวอย่าง

<!DOCTYPE html> <html> <body> <?php 
    $details= array ( 
        'id' => '101', 
        'name' => 'John Smith', 
        'countryName' => 'US' 
    ); 
    $all_details['studentDetails'][] = $details; 
    print_r($all_details); 
?> </body> </html>

ผลลัพธ์

Array ( 
    [studentDetails] => Array ( 
        [0] => Array ( 
            [id] => 101 
            [name] => John Smith 
            [countryName] => US 
        ) 
    ) 
)