PHP Array array_values() Function (With Examples)

In this tutorial, we will learn about the PHP array_values() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_values() function

The array_values() function is used to get the only values from an array, it returns an array of the values with numeric keys (indexing starts with 0).

It is useful, when we need an array with only values.

Syntax

The syntax of the array_values() function:

array_values(arr);

Parameters

The parameters of the array_values() function:

  • arr: It accepts an array and returns a new array with only values.

Return Value

The return type of this method is array, it returns an indexed array of values.

Sample Input/Output

Input:
$arr = array("a" => "Hello", "b" => "Hi!", "c" => "Bye!");

Output:
Array
(    
    [0] => Hello
    [1] => Hi!  
    [2] => Bye! 
)

PHP array_values() Function Example

<?php
//array with keys
$arr = array("a" => "Hello", "b" => "Hi!", "c" => "Bye!");

//creating array with only values
$new_arr = array_values($arr);

//printing
print_r ($new_arr);    
?>

Output

The output of the above example is:

Array
(    
    [0] => Hello
    [1] => Hi!  
    [2] => Bye! 
)

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.