将列数据的多维数组重构为行数据的多维数组 [英] Restructure multidimensional array of column data into multidimensional array of row data

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

问题描述

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

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"
  }
}

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

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