PHP Pushing values into an associative array?


To push values into an associative array, use the brackets [] []. At first create an associative array −

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

The PHP code is as follows to insert values −

Example

 Live Demo

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

Output

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

Updated on: 20-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements