Home > Backend Development > PHP Tutorial > Simple implementation method of converting multi-dimensional array to one-dimensional array in PHP, _PHP tutorial

Simple implementation method of converting multi-dimensional array to one-dimensional array in PHP, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:38
Original
842 people have browsed it

A simple implementation method of converting a multi-dimensional array to a one-dimensional array in PHP,

The example in this article describes the simple implementation method of converting a multi-dimensional array to a one-dimensional array in PHP. Share it with everyone for your reference, the details are as follows:

The PHP language itself does not have a function to convert a multi-dimensional array into a one-dimensional array, but we can write a PHP function ourselves to achieve the function of converting multi-dimensional arrays into one-dimensional arrays.

Using recursion, it is simple and crude. The entire function body implements this function in 9 lines of code. The PHP source code is as follows:

$multi = array(
  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));
    } else {
      $arr[] = $val;
    }
  }
  return $arr;
}

Copy after login

The effect after execution:

Array
(
 [0] => wo
 [1] => shi
 [2] => php
 [3] => cheng
 [4] => xu
 [5] => yuan
 [6] => !
)

Copy after login

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • PHP method to sort a two-dimensional array by specified key value
  • PHP method to delete duplicate elements in an array
  • Analysis of examples of array parameters submitted by php
  • Summary of php array function array_key_exists()
  • PHP multi-dimensional array traversal method (2 implementation methods)
  • Arrays in PHP Summary of some commonly used add, delete, and insert operation functions
  • Detailed explanation of PHP’s definition of arrays and how to create arrays
  • Realizing random merging and sorting of arrays (original sorting) based on PHP
  • How to implement traversal of multi-dimensional arrays in php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084558.htmlTechArticleA simple implementation method of converting PHP multi-dimensional array to one-dimensional array. This article describes the conversion of PHP multi-dimensional array to one-dimensional array. Simple implementation method. Share it with everyone for your reference, the details are as follows: p...
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