如何扁平化阵列阵列阵列? [英] How to flatten array of arrays to array?

查看:137
本文介绍了如何扁平化阵列阵列阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php\">How到&ldquo;&压扁rdquo;的多维数组简单的在PHP?结果
  如何扁平化多维数组?

如果我有这样的code:

if I have this code:

$a = array('a','b',array('c','d'));

我想知道是否有能所有内部数组转换成自己的元素,因此其结果将是一个功能:

I would like to know if there is a function that can convert all inner arrays into their elements so the result would be:

$a = array('a','b','c','d');

所以,我只有内部数组的元素,而不是数组。

So I have only the elements of the inner arrays and not arrays.

推荐答案

我发现的 http://php.net/manual/en/function.array-values​​.php

function array_flatten($array) {
  if (!is_array($array)) {
    return FALSE;
  }
  $result = array();
  foreach ($array as $key => $value) {
    if (is_array($value)) {
      $result = array_merge($result, array_flatten($value));
    }
    else {
      $result[$key] = $value;
    }
  }
  return $result;
} 

这篇关于如何扁平化阵列阵列阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆