PHP 將值推入關聯陣列?


若要將值推入關聯陣列,請使用方括號 [] []。首先建立一個關聯陣列 -

$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
      )
   )
)

更新日期:2020-11-20

2K+ 瀏覽量

開啟你的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.