- 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
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 )
- Related Articles
- PHP Associative Array
- How to add new array elements at the beginning of an array in JavaScript?
- PHP array_push() to create an associative array?
- Convert an object to associative array in PHP
- Remove duplicated elements of associative array in PHP
- How to get numeric index of associative array in PHP?
- PHP Pushing values into an associative array?
- How to build dynamic associative array from simple array in php?
- Add elements at beginning and end of LinkedList in Java
- How to access an associative array by integer index in PHP?
- Swift Program to Add an Element at the Start Or End of the Array
- Associative Arrays in PHP
- C# Program to remove a node at the beginning of a Linked List
- How to add new item in nested array with MongoDB?
- PHP How to add backslash inside array of strings?

Advertisements