dongwuli5105 2011-11-28 03:05
浏览 13
已采纳

更改多维数组的结构

I have some code that creates a multi-dimensional array, but my result seems to be an array of arrays, which is not what I want. I want to flatten this array. How can I change this array result:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [2011-11-18 00:00:00] => C
                )

            [1] => Array
                (
                    [2011-11-18 00:00:00] => I
                )

            [2] => Array
                (
                    [2011-11-18 00:00:00] => S
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [2011-11-22 00:00:00] => C
                )

            [1] => Array
                (
                    [2011-11-22 00:00:00] => S
                )

        )

)

into something that is not an "array of arrays"? Here is the code that creates the array of arrays:

$qrybilled = $this->db->query("SELECT tbltc.BILLED FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario GROUP BY BILLED");
$x = 0; $arr_billed = array();

foreach ($qrybilled->result() as $row) {
$qry = $this->db->query("SELECT tbltc.BILLED, tbltc.TC, tbltc.CAT FROM tbltc WHERE tbltc.PN = $pn AND tbltc.Y = $taxyear AND tbltc.SCENARIO = $scenario AND tbltc.BILLED = '".$row->BILLED."' GROUP BY TC");

    $tmp = array();
    foreach ($qry->result() as $row) {      
        $tmp[] = array( $row->BILLED => $row->TC);
    }
    $arr_billed[] = $tmp; 

}
$data['billed'] = $arr_billed; 

If you can suggest a better way to build the array so that it is not an "array of arrays", that would be great. Thanks for your help.

  • EDIT -

The answer from NickB worked perfectly for me and produced the following output, which is what I was looking for:

Array
(
    [2011-11-18 00:00:00] => Array
        (
            [0] => C
            [1] => I
            [2] => S
        )

    [2011-11-22 00:00:00] => Array
        (
            [0] => C
            [1] => S
        )

)
  • 写回答

2条回答 默认 最新

  • dongzhuo3202 2011-11-28 03:18
    关注

    You can loop through all of the entries in the array to create a new array.

    $result = array();
    
    foreach( $array as $inner_array)
    {
        foreach( $inner_array as $entry)
        {
            foreach( $entry as $key => $value)
            {
                $result[ $key ][] = $value;
            }
        }
    }
    

    Output:

    array(2) {
      ["2011-11-18 00:00:00"]=>
      array(3) {
        [0]=>
        string(1) "C"
        [1]=>
        string(1) "I"
        [2]=>
        string(1) "S"
      }
      ["2011-11-22 00:00:00"]=> 
      array(2) {
        [0]=>
        string(1) "C"
        [1]=>
        string(1) "S"
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义