- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Associative Arrays in PHP
Associative array will have their index as string so that you can establish a strong association between key and values. The associative arrays have names keys that is assigned to them.
Let us see an example−
$arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115");
Above, we can see key and value pairs in the array.
To implement Associative Arrays in PHP, the code is as follows −
Example
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" ); echo "Value 1 = " .reset($arr); ?>
Output
This will produce the following output−
Value 1 = 150
Example
Let us now see another example −
<?php $salaries = array("jack" => 2000, "qadir" => 1000); echo "Salary of jack is ". $salaries['jack'] . "
"; $salaries['jack'] = "high"; $salaries['tom'] = "low"; echo "Salary of jack is ". $salaries['jack'] . "
"; ?>
Output
This will produce the following output−
Salary of jack is 2000 Salary of jack is high
Advertisements