Home » PHP » PHP Examples

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

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

<?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
)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.