本文介绍了将列数据的多维数组重组为行数据的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下列数据的关联数组:

I have the following associative array of column data:

$where = array(
    'id'=>array(
        12,
        13,
        14
    ),
    'date'=>array(
        '1999-06-12',
        '2000-03-21',
        '2006-09-31'
    )
);

我需要将结构转置/旋转为一个行数组(将合并的列数据分配给它们各自的行).我不需要结果中的列名.

I need to transpose / rotate the structure to be an array of rows (with merged column data assigned to their respective row). I don't need the column names in the result.

预期输出:

$comb = array(
    array(12, '1999-06-12'),
    array(13, '2000-03-21'),
    array(14, '2006-09-31')
);

推荐答案

正如Kris Roofe在删除的答案中所述,array_column确实是一种更为优雅的方法.只要确保将其放入某种foreach循环中即可,就像Sahil Gulati向您展示的那样.例如,像这样:

As Kris Roofe stated in his deleted answer, array_column is indeed a more elegant way. Just be sure to put it into some kind of a foreach loop, similar to what Sahil Gulati showed you. For example, like this:

$result = array();

foreach($where['id'] as $k => $v)
{
  $result[] = array_column($where, $k);
}

$resultvar_dump输出正是您要寻找的

The var_dump output of $result is exactly what you're looking for

array(3) {
  [0]=>
  array(2) {
    [0]=>
    int(12)
    [1]=>
    string(10) "1999-06-12"
  }
  [1]=>
  array(2) {
    [0]=>
    int(13)
    [1]=>
    string(10) "2000-03-21"
  }
  [2]=>
  array(2) {
    [0]=>
    int(14)
    [1]=>
    string(10) "2006-09-31"
  }
}

这篇关于将列数据的多维数组重组为行数据的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 10:15