How to get the only values from an associative array in PHP?

PHP array_values() function example: Learn, how to get the only values from an associative array in PHP?
Submitted by IncludeHelp, on February 21, 2019 [Last updated : March 13, 2023]

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

<?php
    //array with keys and values
    $arr = array("name" => "Amit", "age" => 21, "Gender" => "Male");
    
    //creating a new array with values of $arr
    $new_arr = array_values($arr);
    
    //printing new array
    print_r ($new_arr);
?>

Output

Array
(
    [0] => Amit
    [1] => 21
    [2] => Male
)

In the above example, we used the following PHP topics:

PHP Array Programs »

Comments and Discussions!

Load comments ↻





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