Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
707
Q:

PHP array_values() function (with example)

Hey everyone,

I'm relatively new to PHP programming and I came across a function called array_values(), but I'm not quite sure how it works and what it does exactly.

From what I understand, array_values() is a built-in function in PHP that returns an array containing all the values from a given array, while preserving their keys.

I'd like to see some examples to better understand how this function is used. Could someone please provide me with an example or two, along with an explanation of what's happening in the code?

Your help would be greatly appreciated! Thanks in advance.

All Replies

fae89

Hey folks,

I can share my personal experience with the array_values() function in PHP as well. It has been quite useful for me in different projects.

In one project, I had to deal with a multidimensional array where the keys were not important to me, but I needed to work with the values. I didn't want to manually loop through the array and extract the values one by one, so I discovered the array_values() function.

Here's an example to illustrate its usage:

php
$userData = array(
array('John', 25, 'New York'),
array('Emily', 32, 'London'),
array('Michael', 41, 'San Francisco')
);

$valuesOnly = array();

foreach ($userData as $user) {
$valuesOnly[] = array_values($user);
}

print_r($valuesOnly);


In this case, we have a multidimensional array called `$userData`, where each nested array represents user data like name, age, and location.

To extract only the values from each internal array, I used `array_values($user)` within a foreach loop. This allowed me to append the values to a new array called `$valuesOnly`.

Finally, by using `print_r($valuesOnly)`, we can see the output:


Array
(
[0] => Array
(
[0] => John
[1] => 25
[2] => New York
)

[1] => Array
(
[0] => Emily
[1] => 32
[2] => London
)

[2] => Array
(
[0] => Michael
[1] => 41
[2] => San Francisco
)
)


As you can see, the array_values() function helps retrieve only the values from each nested array, which is exactly what I needed in my project.

I hope this example adds to the understanding of how array_values() can be used in practical scenarios. If anyone has more unique examples or insights, feel free to share!

Thanks!

marielle69

Hey there!

Sure, I can definitely share my personal experience with using the array_values() function in PHP.

I often find myself using array_values() when I need to re-index an array or when I only want to work with the values of an array and not the keys. This function comes in handy in such scenarios.

Here's an example to illustrate how array_values() works:


$fruits = array(
'apple' => 'red',
'orange' => 'orange',
'banana' => 'yellow'
);

$valuesOnly = array_values($fruits);

print_r($valuesOnly);


In this example, we have an associative array called `$fruits` with the keys representing the fruit names and the corresponding values representing their colors.

By using `array_values($fruits)`, we are able to extract only the values from the array and store them in a new array called `$valuesOnly`.

Finally, by using `print_r($valuesOnly)`, we can see the output:


Array
(
[0] => red
[1] => orange
[2] => yellow
)


As you can see, array_values() has returned a new array with just the values as its elements, while keeping the original order intact.

I hope this example clarifies how array_values() can be used. It's a nifty function that can be quite useful in various scenarios. If anyone has more examples or insights to share, feel free to jump in!

Thanks!

christopher07

Hey everyone,

I wanted to share my personal experience with the array_values() function in PHP. While the previous responses have covered a lot, I thought I'd add a different perspective on its usefulness.

In one of my projects, I had an array with a specific order of elements. However, I needed to transform this array into a new one while maintaining the original order but without the keys. This is where array_values() came to the rescue.

Here's an example to illustrate my use case:

php
$order = array(
5 => 'Apple',
2 => 'Banana',
9 => 'Orange',
7 => 'Mango'
);

$newOrder = array_values($order);

print_r($newOrder);


In this particular project, the original `$order` array had unique keys assigned to each element, representing a sort of priority. However, I wanted to create a new array that maintained the order of the elements based on the keys, without considering the keys themselves.

By using the array_values() function on the `$order` array, I was able to extract the values while preserving the original order. The result, stored in the `$newOrder` array, looked like this:


Array
(
[0] => Apple
[1] => Banana
[2] => Orange
[3] => Mango
)


As you can see, the array_values() function stripped away the keys and created a new indexed array, maintaining the order of the values from the original array.

I found this functionality quite handy when working with arrays where the order of elements mattered, but the keys were not relevant in my specific use case.

If anyone has encountered a similar scenario or has additional insights to share about array_values(), please do chime in!

Thanks!

New to LearnPHP.org Community?

Join the community