PHP array_values() Function
Quick summary: The PHP array_values() function returns all the values of an array and reindexes it.
PHP array_values() Syntax
array_values(array $array): array
PHP
PHP array_values() Basic examples
var_dump(array_values(['a'=>1, 'b'=>2]));
PHP
Output:
array(2) {
[0]=> int(1)
[1]=> int(2)
}
Extracts values and resets keys.
PHP array_values() Real-world usage
$filtered = array_filter(['a'=>1,'b'=>2]);
var_dump(array_values($filtered));
PHP
Output:
array(2) {
[0]=> int(1)
[1]=> int(2)
}
Reindexes arrays after filtering.
PHP array_values() Edge cases
var_dump(array_values([]));
PHP
Output:
array(0) {
}
Returns an empty array when input is empty.
PHP array_values() Common mistakes
Using array_values unnecessarily
Not always needed if keys matter.
Incorrect
array_values(['a'=>1,'b'=>2]);
Correct
['a'=>1,'b'=>2];
Use only when reindexing is required.
PHP array_values() Frequently Asked Questions
What does array_values() do?
array_values() returns all values from an array and reindexes them.
Does it reset keys?
Yes, numeric keys are reset.
Does it modify original array?
No.
Works with associative arrays?
Yes.
Use case?
Reindexing arrays.
Performance?
Fast.
Can it return empty array?
Yes.
Difference from array_keys()?
Returns values instead of keys.
Handles nested arrays?
Yes.
Can be combined with array_map()?
Yes.
Maintains order?
Yes.
Supports all types?
Yes.
PHP array_values() Related PHP Functions
-
array_allLearn more about array_all
-
array_anyLearn more about array_any
-
array_change_key_caseLearn more about array_change_key_case
-
array_chunkLearn more about array_chunk
-
array_keys PHP examplesLearn more about array_keys PHP examples
-
array_unique PHP examplesLearn more about array_unique PHP examples