Hướng dẫn array_values multidimensional php
Summary: Using PHP array_values in a multidimensional arrayIntroductionThe 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 reviewHere’s the function signature of the array_values.
The function receives an array and returns the retrieved values in an array. An example is as follows.
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 recursivelyAs 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.
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 arrayHere’s a two-dimensional array in PHP.
Here’s the output after recursive PHP array_values.
Test#2 – Three dimensional PHP associative arrayHere’s a three-dimensional array in PHP.
Here’s the output with array_values recursive.
Test#3 – Four dimensional PHP array_valuesHere’s a four-dimensional array in PHP.
So, here’s the output with array_values recursive.
Voila! Three test cases passed. So, that’s convincing enough that the function generalizes well for PHP multidimensional arrays. ConclusionThis 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.
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
|