Skip to content
Home » How to use PHP array_values in a multidimensional array

How to use PHP array_values in a multidimensional array

Summary

If you want to use the array_values function with a multidimensional PHP array, the best approach is to create a recursive function that loops through the levels to call itself and iterate deeper. This allows you to have unlimited array depth.

Using PHP array_values in a multidimensional array

Introduction

The array_values function retrieves the values from an associative array in PHP. It takes an array as an argument and returns an array including the values. The usage is straightforward for a linear associative array. However, things get complicated when PHP multidimensional array factors in. 

A multidimensional array can include numbers and levels of nested arrays, and a hard-coded approach does not go well with complexities. That’s why we seek a more generalized algorithm to use PHP array_values in a multidimensional array, and that’s what precisely this array intends to show you. So, before we come to the main course, let’s quickly review the PHP array_values function.

PHP array_values – A review

Here’s the function signature of the array_values.

array_values(array $array): array

The function receives an array and returns the retrieved values in an array. An example is as follows.

<?php
$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
print_r(array_values($employees_array));
     
/*
OUTPUT
Array
(
    [0] => Bob
    [1] => Stacie
    [2] => Robert
    [3] => Anna
    [4] => Matthew
    [5] => John
)
*/
?>

The array_values returns a numerical index array of the employee array’s values. The array was linear, and a call to the function worked great. But what if we have a multidimensional array? That’s what we are here for today.

Have you tried any of the following highly relevant Pluralsight PHP Courses?

We love Pluralsight because it has thousands of high-quality course content and is extremely affordable. You don’t have to pay for individual courses. It’s a really small monthly membership and you get access to the entire catalog. You can track and review all of your progress and also obtain additional certifications.

Courses

  • PHP Design Patterns
  • PHP: The Big Picture
  • PHP 8: Web Application Security
  • High-Performance PHP
  • Object-Oriented PHP

Learning Paths:

  • PHP Development Fundamentals

Pluralsight Free Trial

Curious about Pluralsight but not ready to commit? We understand. We were able to get a free trial for you to test-drive and see all of their courses. Click on the link below

Try a Pluralsight free trial for 7 days!

Use PHP array_values recursively

As mentioned, a PHP multidimensional array can include any number or level of nested arrays. So, an algorithm should be generalized enough not to care about the structure of an associative array in PHP. Recursion is the way out here. A recursive algorithm can dig deep into any multidimensional array and return a linear array of values.

So, let’s take a look at this recursive array_values algorithm.

function array_values_recursive($arr)
{
   $result = array();
   foreach( array_keys($arr) as $k ){
      $v = $arr[$k];
 
      if (is_scalar($v)) {
         $result[] = $v;
      }
      elseif (is_array($v)) {
         $result = array_merge( $result, array_values_recursive($v));
      }
   }
   return $result;
}

So, the function makes recursive calls if it finds an array and merges it into the result array to form a linear array. Let’s try to run this function over some different PHP multidimensional arrays.

Test#1 – Two dimensional PHP associative array

Here’s a two-dimensional array in PHP.

$arr = [
    ['a','b'],
    ['c', 'd'],
    ['e', 'f'],
    ['g', 'h'],
    ['i', 'j'],
    ['k', 'l'],
    ['m', 'n'],
    ['o', 'p'],
    ['q', 'r'],
    ['s', 't'],
    ['u', 'v'],
    ['w', 'x'],
    ['y', 'z']
];

Here’s the output after recursive PHP array_values.

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [16] => q
    [17] => r
    [18] => s
    [19] => t
    [20] => u
    [21] => v
    [22] => w
    [23] => x
    [24] => y
    [25] => z
)

Test#2 – Three dimensional PHP associative array

Here’s a three-dimensional array in PHP.

$arr = [
    ['1', ['2']],
    ['3', ['4']],
    ['5', ['6']],
    ['7', ['8']],
    ['9', ['10']]
];

Here’s the output with array_values recursive.

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

Test#3 – Four dimensional PHP array_values

Here’s a four-dimensional array in PHP.

$arr = [
    ['A', ['B', ['C']]],
    ['D', ['E', ['F']]],
    ['G', ['H', ['I']]],
    ['J', ['K', ['L']]],
    ['M', ['N', ['O']]],
    ['P', ['Q', ['R']]],
    ['S', ['T', ['U']]],
    ['V', ['W',['X']]],
    ['Y', 'Z']
];

So, here’s the output with array_values recursive.

Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
    [4] => E
    [5] => F
    [6] => G
    [7] => H
    [8] => I
    [9] => J
    [10] => K
    [11] => L
    [12] => M
    [13] => N
    [14] => O
    [15] => P
    [16] => Q
    [17] => R
    [18] => S
    [19] => T
    [20] => U
    [21] => V
    [22] => W
    [23] => X
    [24] => Y
    [25] => Z
)

Voila! Three test cases passed. So, that’s convincing enough that the function generalizes well for PHP multidimensional arrays.

Conclusion

This article focuses on using array_values in a multidimensional array. First of all, the article overviews the array_values function and discusses the challenges with PHP multidimensional array. Finally, it introduces a recursive algorithm and tests some multidimensional arrays. These tests pass, and the algorithm generalizes well for PHP multidimensional arrays.

Hopefully, this article has been of some value to you. You can start learning PHP by following the articles and tutorials at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.