PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age = array["Peter"=>"35", "Ben"=>"37", "Joe"=>"43"];
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
The named keys can then be used in a script:
Example
Try it Yourself »
Loop Through an Associative Array
To loop through and print all the values of an associative array, you could use a foreach
loop, like this:
Example
Try it Yourself »
Complete PHP Array Reference
For a complete reference of all array functions, go to our complete PHP Array Reference.
The reference contains a brief description, and examples of use, for each function!
PHP Exercises
Topic: PHP / MySQLPrev|Next
Answer: Use the Square Bracket []
Syntax
You can simply use the square bracket []
notation to add or push a
key and value pair into a PHP associative array. Let's take a look at an example to understand how it basically works:
Related FAQ
Here are some more FAQ related to this topic:
- How to check if a key exists in an array in PHP
- How to check if a value exists in an array in PHP
- How to get all the keys of an associative array in PHP
- How to get all the values from an associative array in PHP
PHPServer Side ProgrammingProgramming
Kickstart HTML, CSS and PHP: Build a Responsive Website
Featured
59 Lectures 8.5 hours
Ogbemudia Terry Osayawe
More Detail
To create associative arrays in PHP, use [] brackets. You don't need to use array_push[].
Example
Live Demo
Output
Array [ [0] => Array [ [emp_id] => 101 [emp_first_name] => John [emp_last_name] => Doe [emp_country_name] => AUS ] ]
AmitDiwan
Updated on 12-Oct-2020 13:47:40
- Related Questions & Answers
- Creating an associative array in JavaScript with push[]?
- PHP Associative Array
- PHP Pushing values into an associative array?
- Convert an object to associative array in PHP
- How to access an associative array by integer index in PHP?
- How to build dynamic associative array from simple array in php?
- Creating an associative array in JavaScript?
- JavaScript in filter an associative array with another array
- Remove duplicated elements of associative array in PHP
- How to push an array in MongoDB?
- How to get numeric index of associative array in PHP?
- MongoDB query to push document into an array
- Sorting an associative array in ascending order - JavaScript
- Updating an array with $push in MongoDB
- Cannot push into an array from MongoDB?
Previous Page Print Page Next Page
Advertisements
PHPServer Side ProgrammingProgramming
Kickstart HTML, CSS and PHP: Build a Responsive Website
Featured
59 Lectures 8.5 hours
Ogbemudia Terry Osayawe
More Detail
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
Output
Array [ [studentDetails] => Array [ [0] => Array [ [id] => 101 [name] => John Smith [countryName] => US ] ] ]
AmitDiwan
Updated on 20-Nov-2020 05:38:42
- Related Questions & Answers
- PHP Associative Array
- Pushing values into array with multi field set to TRUE?
- PHP array_push[] to create an associative array?
- Convert an object to associative array in PHP
- How to access an associative array by integer index in PHP?
- Creating an associative array in JavaScript?
- Remove duplicated elements of associative array in PHP
- Associative Arrays in PHP
- Merging duplicate values into multi-dimensional array in PHP
- How to build dynamic associative array from simple array in php?
- JavaScript in filter an associative array with another array
- PHP program to split a given comma delimited string into an array of values
- Creating an associative array in JavaScript with push[]?
- Sorting an associative array in ascending order - JavaScript
- How to get numeric index of associative array in PHP?
Previous Page Print Page Next Page
Advertisements