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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)