Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP

Bring order back to your PHP arrays using array_values()

Table of contents:

Bring order back to your PHP arrays using array_values()

Talking about PHP, it’s hard not to dive into its array handling capabilities. 😅 One particularly handy function is array_values(). Ever found yourself needing to re-index an array? That’s where array_values() shines.

What is array_values() in PHP?

In PHP, array_values() is a built-in function that returns all the values from an array and indexes them numerically. It’s perfect when you’re not concerned about the keys but just want the values in a simple, 0-indexed array.

How does array_values() work?

Imagine you have an associative array where the keys are all over the place. Maybe they’re strings, or perhaps they’re just not in the order you need. If you only need the values, array_values() simplifies this by stripping away the keys and providing a neatly indexed array.

Simple examples for array_values()

Example #1

Let’s say you have an array like this:

$fruits = [
'apple' => 'Apple',
'orange' => 'Orange',
'banana' => 'Banana'
];

If you apply array_values() to this array:

array_values($fruits);

You’ll get a simple array back:

[
0 => 'Apple',
1 => 'Orange',
2 => 'Banana',
]

Notice how the associative keys are gone, and the values are indexed from 0 onwards.

Example #2

Another great use case is when you sort your array alphabetically using the sort() function, which can mess up the order of your key. Let me show you:

$fruits = [
'Orange',
'Banana',
'Apple',
];
 
sort($fruits);
 
// [
// 3 => 'Apple',
// 2 => 'Banana',
// 1 => 'Orange',
// ]
var_dump($fruits);

As you can see, the numerical keys are not in ascending order anymore. But if you apply array_values() to this array, you’ll get a nice, clean array back:

array_values($fruits);
 
// [
// 0 => 'Apple',
// 1 => 'Banana',
// 2 => 'Orange',
// ]
var_dump($fruits);
Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

Learn how to sort any kind of array in PHP Learn how to sort any kind of array in PHP

Let me walk you through some of the most useful functions in PHP that will enable you to sort any kind of array.

Published on Nov 9, 2023

PHP
Print an array with PHP (+ Laravel) Print an array with PHP (+ Laravel)

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP (and even Laravel-specific helpers).

Modified on Jun 24, 2023

Understanding array_filter() in PHP Understanding array_filter() in PHP

See how PHP allows you to filter unwanted values in arrays in a simple and concise way.

Published on Nov 11, 2023

PHP
The fastest way to check if your PHP array is empty The fastest way to check if your PHP array is empty

There are multiple ways to check if an array is empty. Let me tell you about each of them and why and when you should use them.

Modified on Nov 2, 2023 Audio available

PHP
Easily convert a PHP array to JSON Easily convert a PHP array to JSON

Convert PHP arrays to JSON with `json_encode()`. Ideal for data exchange, storing data, and API communication.

Published on Sep 16, 2023

PHP
console.log() in PHP console.log() in PHP

Explore the world of PHP debugging with var_dump(), and Laravel's friendlier alternatives, dump() and dd(). Much charm, such useful!

Modified on Sep 6, 2023

PHP

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -