php

PHP array_values() Function

array_values() in PHP will return all the values from an input array. It returns all the values in a new array. It takes the input array as the only parameter.

Syntax:
array_values(array_input)

Parameter:
array_input is the array

Let’s see examples to understand this concept better.

Example 1:

In this example, we will create an array with four values: ‘lotus’,’lilly’,’jasmine’,’rose’.

Let’s return all the values from the array.

<?php

//create an array named Flower1 with 4 values.
$Flower=array('lotus','lilly','jasmine','rose');

echo "Actual Array: ";
print_r($Flower);

//return values

echo "Values: ";
print_r(array_values($Flower));

?>

Output:

We can see that all the values are returned with default keys that start from 0.

Example 2:
In this example, we will create an array with four key-values: ‘flower1’=>’lotus’,’flower2’=>’lilly’,’flower3’=>’jasmine’,’flower4’=>’rose’.

Let’s return all the values from an array.

<?php

//create an array named Flower with 4 key-values.
$Flower=array('flower1'=>'lotus','flower2'=>'lilly','flower3'=>'jasmine','flower4'=>'rose');

echo "Actual Array: ";
print_r($Flower);

//return values

echo "Values: ";
print_r(array_values($Flower));

?>

Output:

We can see that all the values are returned.

Example 3:

In this example, we will create an array with four key-values: ‘flower1’=>’null’,’flower2’=>’null’,’flower3’=>’null’,’flower4’=>’null’. All values are null.

Let’s return all the values from an array.

<?php

//create an array named Flower with 4 key-values.
$Flower=array('flower1'=>null,'flower2'=>null,'flower3'=>null,'flower4'=>null);

echo "Actual Array: ";
print_r($Flower);

//return values

echo "Values: ";
print_r(array_values($Flower));

?>

Output:

We can see that empty values are returned since the values are null in the array.

Conclusion

In this article, we saw how to return all the values into a new array using the array_values() function in PHP. If the value is null, then the array_values() function return empty(nothing).

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain