PHP Associative Array PHP
- PHP Tutorial
- What is PHP
- Install PHP
- PHP Example
- PHP Echo
- PHP Print
- PHP Variables
- PHP $ and $$ Variables
- PHP Constants
- Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- PHP Boolean
- PHP is_bool() function
- PHP Integer
- PHP is_int() function
- PHP Float
- PHP is_float Function
- PHP Compound Types
- PHP Special Types
- PHP is_null() function
- PHP If Else
- PHP Switch
- PHP For Loop
- PHP While Loop
- PHP do while loop
- PHP Break
- PHP Functions
- PHP Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Argument Values Function
- PHP Variable Length Argument Function
- PHP Recursive Function
- PHP Arrays
- PHP Indexed Array
- PHP Associative Array
- PHP Multidimensional Array
- PHP Array Functions
- PHP String
- PHP String Functions
- PHP Math functions
- PHP Form Handling | GET and POST method
- PHP Include File
- PHP Cookie
- PHP Session
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append to File
- PHP Delete File
- PHP File Upload
- PHP Download File
- PHP Mail
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table Example
- PHP MySQL Insert Record
- PHP Prepared Statements
- PHP MySQL Update Record
- PHP MySQL Delete Record
- PHP MySQL Select
- PHP MySQL Order By
- PHP Limit Data MySql
- PHP JSON
- PHP XML Parsers
- PHP SimpleXML Parser
- PHP SimpleXML - Get Node/Attribute Values
- PHP XML Expat Parser
- PHP XML DOM Parser
- PHP - AJAX intro
- MySQL CREATE VIEW
PHP Associative Array
PHP Associative Array
PHP allows you to associate name/label with each array elements in PHP using => symbol. Such way, you can easily remember the element because each element is represented by label than an incremented number.
Definition
There are two ways to define associative array:
1st way:
- $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
2nd way:
- $salary["Sonoo"]="550000";
- $salary["Vimal"]="250000";
- $salary["Ratan"]="200000";
Example
File: arrayassociative1.php
- <?php
- $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
- echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
- echo "Vimal salary: ".$salary["Vimal"]."<br/>";
- echo "Ratan salary: ".$salary["Ratan"]."<br/>";
- ?>
Output:
Sonoo salary: 550000
Vimal salary: 250000
Ratan salary: 200000
Vimal salary: 250000
Ratan salary: 200000
File: arrayassociative2.php
- <?php
- $salary["Sonoo"]="550000";
- $salary["Vimal"]="250000";
- $salary["Ratan"]="200000";
- echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
- echo "Vimal salary: ".$salary["Vimal"]."<br/>";
- echo "Ratan salary: ".$salary["Ratan"]."<br/>";
- ?>
Output:
Sonoo salary: 550000
Vimal salary: 250000
Ratan salary: 200000
Vimal salary: 250000
Ratan salary: 200000
Traversing PHP Associative Array
By the help of PHP for each loop, we can easily traverse the elements of PHP associative array.
- <?php
- $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
- foreach($salary as $k => $v) {
- echo "Key: ".$k." Value: ".$v."<br/>";
- }
- ?>
Output:
Key: Sonoo Value: 550000
Key: Vimal Value: 250000
Key: Ratan Value: 200000
Key: Vimal Value: 250000
Key: Ratan Value: 200000