A method Instance _php 3 PHP multidimensional array to one-dimensional array

Source: Internet
Author: User
Tags arrays

Many times we need to turn multidimensional arrays into one-dimensional arrays, because we only need one-dimensional arrays, and a one-dimensional array is more convenient to use, how do you turn a multidimensional array into a one-dimensional array in PHP? Let's take a look at three multidimensional arrays as examples of one-dimensional arrays:
First, use foreach

Copy Code code as follows:
<?php
function Arr_foreach ($arr)
{
Static $tmp =array ();
if (!is_array ($arr))
{
return false;
}
foreach ($arr as $val)
{
if (Is_array ($val))
{
Arr_foreach ($val);
}
Else
{
$tmp []= $val;
}
}
return $tmp;
}
$a = Array (1,2=>array (3,4=>array (5,6)), 7);
Print_r (Arr_foreach ($a));
?>

using a For loop, you can only traverse the array of numeric subscripts
Copy Code code as follows:
<?php
function Arr_foreach ($arr)
{
Static $tmp =array ();

for ($i =0; $i <count ($arr); $i + +)
{
if (Is_array ($arr [$i]))
{
Arr_foreach ($arr [$i]);
}else{
$tmp []= $arr [$i];
}
}

return $tmp;
}
Call Example
$a = Array (1,array (3,array (5,6)), 7);
Print_r (Arr_foreach ($a));
?>

third, use while
Copy Code code as follows:
/**
* Convert a multidimensional array to a one-dimensional array
* @author Echo
* @link http://www.jb51.net/
* @param array $arr
* @return Array
*/
function Arrmd2ud ($arr) {
#将数值第一元素作为容器, to assign a value to the address.
$ar _room = & $arr [key ($arr)];
#第一容器不是数组进去转呀
if (!is_array ($ar _room)) {
#转为成数组
$ar _room = Array ($ar _room);
}
#指针下移
Next ($arr);
#遍历
while (the list ($k, $v) = each ($arr)) {
#是数组就递归深挖, not just the group.
$v = Is_array ($v)? Call_user_func (__function__, $v): Array ($v);
#递归合并
$ar _room = array_merge_recursive ($ar _room, $v);
#释放当前下标的数组元素
Unset ($arr [$k]);
}
return $ar _room;
}

Call Example:

Copy Code code 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 code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.