- 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
How to access an associative array by integer index in PHP?
To access an associative array by integer index in PHP, the code is as follows−
Example
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110"); $keys = array_keys( $arr ); echo "Array key and value...
"; for($x = 0; $x < sizeof($arr); $x++ ) { echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "
"; } ?>
Output
This will produce the following output−
Array key and value... key: p, value: 150 key: q, value: 100 key: r, value: 120 key: s, value: 110
Example
Let us now see another example−
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110"); $keys = array_keys( $arr ); echo "Array key and value...
"; for($x = 0; $x < sizeof($arr); $x++ ) { echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "
"; } $arr[$keys[2]] = "20"; $arr[$keys[3]] = "10"; echo "
Updated Array key and value...
"; for($x = 0; $x < sizeof($arr); $x++ ) { echo "key: ". $keys[$x] . ", value: ". $arr[$keys[$x]] . "
"; } ?>
Output
This will produce the following output−
Array key and value... key: p, value: 150 key: q, value: 100 key: r, value: 120 key: s, value: 110 Updated Array key and value... key: p, value: 150 key: q, value: 100 key: r, value: 20 key: s, value: 10
- Related Articles
- How to get numeric index of associative array in PHP?
- Convert an object to associative array in PHP
- PHP array_push() to create an associative array?
- PHP Associative Array
- PHP Pushing values into an associative array?
- How to re-index an array in PHP?
- How to generate array key using array index – JavaScript associative array?
- How to build dynamic associative array from simple array in php?
- How to delete an element from an array in PHP and re-index the array?
- How to access index of an element in jQuery?
- Remove duplicated elements of associative array in PHP
- Creating an associative array in JavaScript?
- How to remove an array element by its index in MongoDB?
- Associative Arrays in PHP
- How to Use Associative Array in TypeScript?

Advertisements