A method instance of converting 3 PHP multidimensional arrays into one-dimensional arrays

Source: Internet
Author: User
Tags foreach array example arrays

  This article mainly introduces 3 PHP multidimensional array to a one-dimensional array of methods, respectively, using foreach, for, while three kinds of loop method to achieve, the need for friends can refer to the following

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 to a one-dimensional array example: First, use the foreach   code as follows: <?php    function Arr_foreach ($arr)      {& nbsp     Static $tmp =array ();         if (!is_array ($arr))         {         return FA Lse      }       foreach ($arr as $val)         {      &NBS P  if (Is_array ($val))            {            Arr_foreach ($val);          }           else           {             $tmp []= $val;           {     }       return $tmp;   &NBSP}     $a = Array (1,2=>array (3,4=>array (5,6)), 7);    print_r (Arr_foreach ($a));; Second, use for loop, can only traverse the number subscript array   code as follows: <?php    function Arr_foreach ($arr)    {      Static $tmp =array ();           for ($i =0 $i <count ($arr) $i + +)       {        & nbsp  if (Is_array ($arr [$i]))          {              Arr_fore Ach ($arr [$i]);            }else{              $tmp []= $arr [$i];   & nbsp                 {          return $tmp;      }     //Call example     $a = Array (1,array (3,array (5,6)), 7);    print_r (Arr_foreach ($a));?> third, use while   code as follows:/**  * convert multidimensional array to one-dimensional array  * @author echo  * @l Ink http://www.jb51.net/ * @param array $arr  * @return array  */function Arrmd2ud ($arr) {  #将数值第一元素 As a container, the address is assigned a value.  $ar _room = & $arr [key ($arr)];   #第一容器不是数组进去转呀  if (!is_array ($ar _room)) {  #转为成数组   $ar _room = Array ($ar _room);     #指针下移  next ($arr);   #遍历  while (list ($k, $v) = each ($arr)) {  #是数组就递归深挖, not on the group   $v = Is_array ($v)? Call_user_func (__f unction__, $v): Array ($v);   #递归合并   $ar _room = array_merge_recursive ($ar _room, $v);   #释放当前下标的数组元素   unset ($arr [$k]); &NBSP}  return $ar _room;   Call Example:   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:   Code as follows: Array (    [0] => 1     [1] => 2     [2] => 1     [3] =& Gt 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.