3 Examples of Methods to Convert Multidimensional Arrays to One-Dimensional Arrays in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:36:01
Original
661 people have browsed it

Many times we need to convert multi-dimensional arrays into one-dimensional arrays, because we only need one-dimensional arrays, and one-dimensional arrays are more convenient to use. How to convert multi-dimensional arrays into one-dimensional arrays in PHP? Let’s take a look at three examples of converting multi-dimensional arrays into one-dimensional arrays:
1. Use foreach

Copy code code As follows:
function arr_foreach ($arr)
{
static $tmp=array();
if (!is_array ($arr))
                                       return false;                                                               arr_foreach ($val) ;
                                                                            > }
$a = array (1,2=>array(3,4=>array(5,6)),7);
print_r(arr_foreach($a));
?>


2. Using a for loop, you can only traverse the array of numerical subscripts




Copy the code

The code is as follows:

< ?php
function arr_foreach($arr)
{
static $tmp=array();
for($i=0; $i                                                                                   $tmp[] =$arr[$i]; } }
} } return $tmp; } //Calling example
$a = array(1,array(3 ,array(5,6)),7);
print_r(arr_foreach($a));
?>


3. Use while




Copy code

The code is as follows:

/**
* Convert multi-dimensional array to one-dimensional array
* @author echo
* @link http://www.jb51.net/
* @param array $arr
* @return array
*/
function ArrMd2Ud($arr) {
#Change the value An element serves as a container for address assignment.
$ar_room = &$arr[key($arr)]; #The first container is not an array. if (!is_array($ar_room)) {
#Convert to an array
$ar_room = array($ar_room); } #Move the pointer down next($arr); #Traverse
while (list($k, $v) = each($arr)) {
#If it is an array, dig deep recursively, if not, convert it to an array
$v = is_array($v) ? call_user_func(__FUNCTION__, $v) : array($v);
#Recursive merge
$ar_room = array_merge_recursive($ar_room, $v);
#Release the array element of the current subscript
unset($arr[$k]);
}
return $ar_room;
}


Call example:



Copy code

The code is as follows:


$arr = array(1, 2, 3 => ; array(1, 2, 'ar' => array(1, 2 => array('a', 'b'))), array('ar' => array(3, 4))) ;
print_r(ArrMd2Ud($arr));

Output:



Copy code
The code is as follows:


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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/741257.htmlTechArticleMany times we need to convert multi-dimensional arrays into one-dimensional arrays, because we only need one-dimensional arrays, and one-dimensional Arrays are more convenient to use. How to convert multi-dimensional arrays into one-dimensional numbers in PHP...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!