search
Homephp教程php手册PHP多维数组转一维数组的简单实现方法,

PHP多维数组转一维数组的简单实现方法,

本文实例讲述了PHP多维数组转一维数组的简单实现方法。分享给大家供大家参考,具体如下:

php语言本身没有将多维数组转为一维数组的函数,但是我们可以自己写一个php函数来实现将多维转一维的功能。

运用了递归,简单粗暴,整个函数体9行代码就实现了该功能,php源码如下:

$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;
}

执行后的效果:

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

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP按指定键值对二维数组进行排序的方法
  • php删除数组中重复元素的方法
  • php提交post数组参数实例分析
  • php数组函数array_key_exists()小结
  • PHP多维数组遍历方法(2种实现方法)
  • PHP中对数组的一些常用的增、删、插操作函数总结
  • 详解PHP对数组的定义以及数组的创建方法
  • 基于php实现随机合并数组并排序(原排序)
  • php实现遍历多维数组的方法
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)