PHP array_push() to create an associative array?

PHPServer Side ProgrammingProgramming

To create associative arrays in PHP, use [] brackets. You don't need to use array_push().

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
$emp= (object) [
   'employeeId'=>"101",
   'employeeFirstName'=>"John",
   'employeeLastName'=>"Doe",
   'employeeCountryName'=>"AUS"
];
$employeeDetails[] = [
   'emp_id' => $emp->employeeId,
   'emp_first_name' => $emp->employeeFirstName,
   'emp_last_name' => $emp->employeeLastName,
   'emp_country_name' => $emp->employeeCountryName
];
print_r(array_values($employeeDetails));
?>
</body>
</html>

Output

Array ( [0] => Array ( [emp_id] => 101 [emp_first_name] => John [emp_last_name] => Doe [emp_country_name] => AUS ) )
raja
Published on 12-Oct-2020 17:17:08
Advertisements