
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 program to add item at the beginning of associative array
To add an item at the beginning of the associative array, 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 "Initial Array...
"; print_r($arr); array_unshift($arr,"100"); echo "Updated Array...
"; print_r($arr); ?>
Output
This will produce the following outputβ
Initial Array... Array( [p] => 150 [q] => 100 [r] => 120 [s] => 110 [t] => 115 [u] => 103 [v] => 105 [w] => 125 ) Updated Array... Array ( [0] => 100 [p] => 150 [q] => 100 [r] => 120 [s] => 110 [t] => 115 [u] => 103 [v] => 105 [w] => 125 )
Example
Let us now see another exampleβ
<?php $arr = array( 0=>"Ryan", 1=>"Kevin", 2=>"Jack" ); echo "Initial Array...
"; print_r($arr); array_unshift($arr,"Katie"); echo "Updated Array...
"; print_r($arr); ?>
Output
This will produce the following outputβ
Initial Array... Array( [0] => Ryan [1] => Kevin [2] => Jack ) Updated Array... Array( [0] => Katie [1] => Ryan [2] => Kevin [3] => Jack )
Advertisements