Yes, use array_values
.
array_values[array['0' => '101', '1' => '105', '2' => '103']]; // returns array[101, 105, 103]
Edit: [Thanks to @MarkBaker]
If you use var_dump
on the original array and the "values only" array the output might look exactly the same if the keys are numerical and and ascending beginning from 0. Just like in your example.
If the keys are not consisting of numbers or if the numbers are "random" then the output would be different. For example if the array looks like
array['one' => '101', 'two' => '105', 'three' => '103']
the output of var_dump
looks different after converting the array with array_values
.
Home » PHP » PHP programs
PHP array_values[] function example: Here, we are going to learn how to get the only values from an associative array in PHP?
Submitted by IncludeHelp, on February 21, 2019
Given an array with keys, values and we have to create an array with the only values.
Getting the only values from an array
To extract the only values from an associative array, we use array_values[] function, it returns a new arrays with numeric indexes [starting from 0 index] and values.
Syntax:
array_values[array];
It accepts an array and returns a new array having only values from given array.
PHP code to get the only values from an associative array
Output
Array [ [0] => Amit [1] => 21 [2] => Male ]
PHP Array Programs »
[PHP 4, PHP 5, PHP 7, PHP 8]
array_values — Return all the values of an array
Description
array_values[array $array
]: array
Parameters
array
The array.
Return Values
Returns an indexed array of values.
Examples
Example #1 array_values[] example
The above example will output:
Array [ [0] => XL [1] => gold ]
See Also
- array_keys[] - Return all the keys or a subset of the keys of an array
- array_combine[] - Creates an array by using one array for keys and another for its values
biziclop at vipmail dot hu ¶
8 years ago
Remember, array_values[] will ignore your beautiful numeric indexes, it will renumber them according tho the 'foreach' ordering:
nopy at users dot sourceforge dot net ¶
18 years ago
Just a warning that re-indexing an array by array_values[] may cause you to reach the memory limit unexpectly.
For example, if your PHP momory_limits is 8MB,
and says there's a BIG array $bigArray which allocate 5MB of memory.
Doing this will cause PHP exceeds the momory limits:
It's because array_values[] does not re-index $bigArray directly,
it just re-index it into another array, and assign to itself later.
abimaelrc ¶
11 years ago
This is another way to get value from a multidimensional array, but for versions of php >= 5.3.x
bluej100 at gmail dot com ¶
15 years ago
Most of the array_flatten functions don't allow preservation of keys. Mine allows preserve, don't preserve, and preserve only strings [default].
Anonymous ¶
18 years ago
code till dawn! -mark meves!
chrysb at gmail dot com ¶
14 years ago
If you are looking for a way to count the total number of times a specific value appears in array, use this function:
This should really be a native function of PHP.