
- 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
Return an array with numeric keys PHP?
For this, use foreach loop along with [] and implement arary_values() method.
Example
<!DOCTYPE html> <html> <body> <?php $details = [ [ "id" => "10001", "firstName" => "John", "lastName" => "Doe" ], [ "id" => "10002", "firstName" => "David", "lastName" => "Miller" ] ]; foreach ($details as $k=> $v) { $someDetails[$v['id']][] = ['LASTNAME' => $v['lastName'], 'FIRSTNAME'=> $v['firstName']]; } print_r(array_values($someDetails)); ?> </body> </html>
Output
Array ( [0] => Array ( [0] => Array ( [LASTNAME] => Doe [FIRSTNAME] => John ) ) [1] => Array ( [0] => Array ( [LASTNAME] => Miller [FIRSTNAME] => David ) ) )
- Related Articles
- PHP – Return an array of all supported encodings with mb_list_encodings()
- JavaScript: replacing object keys with an array
- PHP script to get all keys from an array that starts with a certain string
- Reset keys of array elements using PHP ?
- PHP print keys from an object?
- Sort multidimensional array by multiple keys in PHP
- Return all dates between two dates in an array in PHP
- How to get numeric index of associative array in PHP?
- Return the numeric string left-filled with zeros in Numpy
- Comparing objects in JavaScript and return array of common keys having common values
- How can we fetch all the data from MySQL table by using mysql_fetch_array() function, returning an array with the numeric index, in PHP script?
- PHP return Statement
- Recursively loop through an array and return number of items with JavaScript?
- How to convert Map keys to an array in JavaScript?
- Find the GCD of an array made up of numeric strings

Advertisements