- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP array_push() to create an associative array?
To create associative arrays in PHP, use [] brackets. You don't need to use array_push().
Example
<!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 ) )
- Related Articles
- Convert an object to associative array in PHP
- PHP Associative Array
- PHP Pushing values into an associative array?
- array_push() function in PHP
- How to access an associative array by integer index in PHP?
- How to build dynamic associative array from simple array in php?
- Remove duplicated elements of associative array in PHP
- How to get numeric index of associative array in PHP?
- PHP program to add item at the beginning of associative array
- Creating an associative array in JavaScript?
- Associative Arrays in PHP
- JavaScript in filter an associative array with another array
- Creating an associative array in JavaScript with push()?
- Sorting an associative array in ascending order - JavaScript
- How to create comma separated list from an array in PHP?

Advertisements