Home >Backend Development >PHP Tutorial >如何合并这个二维数组?

如何合并这个二维数组?

WBOY
WBOYOriginal
2016-06-06 20:15:531064browse

请看下面的二维数组(php语言)

<code>array(
0 => 
  array (
    0 => 'a1',
    1 => 'b1',
    2 => 'c1',

),
1 => 
  array (
    0 => 'a2',
    1 => 'b2',
    2 => 'c2',

),
2 => 
  array (
    0 => 'a3',
    1 => 'b3',
    2 => 'c3',

),

)</code>

合并成

<code>array(
0 => 
  array (
    0 => 'a1',
    1 => 'a2',
    2 => 'a3',

),
1 => 
  array (
    0 => 'b1',
    1 => 'b2',
    2 => 'b3',

),
2 => 
  array (
    0 => 'c1',
    1 => 'c2',
    2 => 'c3',

),

)</code>

请指点,谢谢。

回复内容:

请看下面的二维数组(php语言)

<code>array(
0 => 
  array (
    0 => 'a1',
    1 => 'b1',
    2 => 'c1',

),
1 => 
  array (
    0 => 'a2',
    1 => 'b2',
    2 => 'c2',

),
2 => 
  array (
    0 => 'a3',
    1 => 'b3',
    2 => 'c3',

),

)</code>

合并成

<code>array(
0 => 
  array (
    0 => 'a1',
    1 => 'a2',
    2 => 'a3',

),
1 => 
  array (
    0 => 'b1',
    1 => 'b2',
    2 => 'b3',

),
2 => 
  array (
    0 => 'c1',
    1 => 'c2',
    2 => 'c3',

),

)</code>

请指点,谢谢。

<code>$arr = array_map(function($key) use($arr) { 
    return array_column($arr, $key);
}, array_keys($arr[0]));</code>

我一下想到的只有遍历合并,然后搜了一下没有现成的方法,静等大神。

对角线转置矩阵。我能想到的是遍上三角或者下三角。进行交换。a ij 和a ji

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