Hướng dẫn array_values multidimensional php

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
    • PHP array_values – A review
    • Use PHP array_values recursively
      • Test#1 – Two dimensional PHP associative array
      • Test#2 – Three dimensional PHP associative array
      • Test#3 – Four dimensional PHP array_values
    • Conclusion
    • Want to learn more about PHP?
    • Can Your Career Use a Boost?
      • Related posts:

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.

 "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.

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.

  • How to preserve keys when using array_chunk in PHP
  • How to recursively convert object to array in PHP
  • Three reasons why PHP is still alive & relevant in 2022
  Was this information helpful?

Can Your Career Use a Boost?

Check out our free PHP developer career guide. We created this guide as a resource to help fellow programmers level up their careers. This is an excellent resource whether you are new to programming or have been a professional for more than 15 years.

Click here to read the guide

Level up your Programming Career.

Check out our online programming career guide where we continuously update content to help you take your career to the next level. This is applicable & actionable content for those who are in years 2-5 as well as those of us who are in year 15+.

Programming Career Guide


  • ← How to loop through a multidimensional array in PHP
  • How to recursively convert object to array in PHP →