Sunday, November 17, 2024
Google search engine
HomeLanguagesPHP array_values() Function

PHP array_values() Function

In this article, we will see how to get an array value using the array_values() function in PHP, along with understanding their implementation through the example.

The  array_values() is an inbuilt PHP function is used to get an array of values from another array that may contain key-value pairs or just values. The function creates another array where it stores all the values and by default assigns numerical keys to the values.

Syntax:

array array_values($array)

Parameters: This function takes only one parameter $array which is mandatory and refers to the original input array, from which values need to be fetched.

Return Value: This function returns an array with the fetched values, indexed with the numerical keys.

Consider the following examples. Here, we are using the concept of Associative arrays that are used to store key-value pairs.

Input: $array = ("ram"=>25, "krishna"=>10, "aakash"=>20, "gaurav")
Output:
Array
(
    [0] => 25
    [1] => 10
    [2] => 20
    [3] => gaurav
)
Explanation: This array use named keys that we have assigned to them. 
For those array element that has not assigned any value, will display 
their index value.

Input: $array = ("ram", "krishna", "aakash", "gaurav")
Output:
Array
(
    [0] => ram
    [1] => krishna
    [2] => aakash
    [3] => gaurav
)
Explanation: This array displays the array item along with index value.

Example: The below example illustrates the array_values() function in PHP.

PHP




<?php
  
  // PHP function to illustrate the use of array_values()
  function Return_Values($array)
  {
      return (array_values($array));
  }
  $array = array("ram"=>25, "krishna"=>10, "aakash"=>20, "gaurav");
  print_r(Return_Values($array));
?>


Output:

Array
(
    [0] => 25
    [1] => 10
    [2] => 20
    [3] => gaurav
)

Reference: http://php.net/manual/en/function.array-values.php

RELATED ARTICLES

Most Popular

Recent Comments