A simple method of realizing the one-dimensional array of PHP multidimensional array _php tips

Source: Internet
Author: User

This paper illustrates a simple way to realize the one-dimensional array of PHP multidimensional arrays. Share to everyone for your reference, specific as follows:

The PHP language itself does not convert multidimensional arrays into one-dimensional arrays, but we can write a PHP function of our own to achieve the function of turning multidimensional into one dimension.

The use of recursion, simple and rough, the entire function body 9 lines of code to achieve this function, PHP source code is as follows:

$multi = Array (Array (
      ' wo ',
      ' Shi '
    ),
    ' php '
  ),
  ' Cheng ',
  Array (
    Array (
      ' Xu ',
      ' yuan ',
    )
  ),
  '! '
);
$multi = Arrtoone ($multi);
Print_r ($multi);
function Arrtoone ($multi) {
  $arr = array ();
  foreach ($multi as $key => $val) {
    if (Is_array ($val)) {
      $arr = Array_merge ($arr, Arrtoone ($val));
    SE {
      $arr [] = $val;
    }
  }
  return $arr;
}

Effect after execution:

Array
(
 [0] => wo
 [1] => shi
 [2] => PHP
 [3] => Cheng
 [4] => Xu
 [5] => ; Yuan
 [6] =>!
)

I hope this article will help you with the PHP program design.

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.