
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to re-index an array in PHP?
To re-index an array in PHP, the code is as follows −
Example
<?php $arr = array( 0=>"150", 1=>"100", 2=>"120", 3=>"110", 4=>"115" ); echo "Array...
"; foreach( $arr as $key => $value) { echo " Key = " . $key . ", Value = " . $value . "
"; } $arr = array_combine(range(2, count($arr) + (1)), array_values($arr)); echo "
Array after re-indexing
"; foreach( $arr as $key => $value) { echo " Key = " . $key . ", Value = " . $value . "
"; } ?>
Output
This will produce the following output−
Array... Key = 0, Value = 150 Key = 1, Value = 100 Key = 2, Value = 120 Key = 3, Value = 110 Key = 4, Value = 115 Array after re-indexing Key = 2, Value = 150 Key = 3, Value = 100 Key = 4, Value = 120 Key = 5, Value = 110 Key = 6, Value = 115
Example
Let us now see another example −
<?php $arr = array( "a"=>"150", "b"=>"100", "c"=>"120", "d" =>"110", "e"=>"115" ); echo "Array...
"; foreach( $arr as $key => $value) { echo " Key = " . $key . ", Value = " . $value . "
"; } $arr = array_combine(range("b", chr(count($arr) + (ord("b")-1))), array_values($arr)); echo "
Array after re-indexing
"; foreach( $arr as $key => $value) { echo " Key = " . $key . ", Value = " . $value . "
"; } ?>
Output
This will produce the following output−
Array... Key = a, Value = 150 Key = b, Value = 100 Key = c, Value = 120 Key = d, Value = 110 Key = e, Value = 115 Array after re-indexing Key = b, Value = 150 Key = c, Value = 100 Key = d, Value = 120 Key = e, Value = 110 Key = f, Value = 115
- Related Articles
- How to delete an element from an array in PHP and re-index the array?
- How to access an associative array by integer index in PHP?
- Removing Array Element and Re-Indexing in PHP
- How to get numeric index of associative array in PHP?
- How to convert an array to SimpleXML in PHP?
- How to trim all strings in an array in PHP ?
- How to remove an array element by its index in MongoDB?
- In PHP, how can I add an object element to an array?
- Convert object to an array in PHP.
- How to get random value out of an array in PHP?
- How to get the first element of an array in PHP?
- How to create comma separated list from an array in PHP?
- How to delete an array element based on key in PHP?
- How to check for multidimensional nature of an array in PHP
- PHP How to display array values with text “even” to be displayed for even index values

Advertisements