PHP array_values Function | Return Values of Array

PHP array_values is an inbuilt Function in PHP. It returns all the values of an array. It takes an array in the input and returns another array containing all the values in the array. However, it returns the array only up to the first level. In this article, we will discuss the PHP array_values Function. Also, we will discuss a few examples.

Syntax

array array_values($array)

Parameters

The PHP array_values function expects only one mandatory parameter which is the original array for which the function will fetch the values.

Return Value

The PHP array_values Function creates another array in which it stores the values of the original array and returns it.

The Function returns any nested array as it is and does not process it.

PHP array_values Function Diagram

Examples

Let’s discuss a few examples to demonstrate the use of array_values Function in PHP.

Example 1: Basic Usage

<?php
	
	$testArray = array(
		'apple' => 1,
		'mango' => 24,
		5 => 10,
		'grapes' => array(
			'black' => 1,
			'green' => 2
		)
	);

	$values = array_values($testArray);
	print_r($values);
	/*
	Array
	(
	    [0] => 1
	    [1] => 24
	    [2] => 10
	    [3] => Array
	        (
	            [black] => 1
	            [green] => 2
	        )

	)
	*/
?>

In the above example, an array with mixed keys is passed to the array_values. You can observe the following things:

  • The array_values Function assigns default numeric keys in the final array. It discards all the original keys of the array.
  • Also, it returns the nested array as it is. The output array still contains the original keys of the inner array (grapes -> black/green).

Conclusion

In conclusion, we discussed the array_values Function. It is useful in retrieving values from an array. You can read more about it on the Official Documentation of PHP. You can also browse through other PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *