dongpu1331 2011-06-10 16:34
浏览 54
已采纳

输出二维数组

Ok, I'm still struggling with my arrays... I have created a two-dimensional array and saved into a session to get results on another page: $_SESSION['myARRAY']

print_r($_SESSION['myARRAY']);

// will output:
Array ( [Car] => Array ( [0] => 1 [1] => 9 [2] => 0 ) 
        [Truck] => Array ( [0] => 2 [1] => 10 [2] => 0 ) 
        [Bus] => Array ( [0] => 1 [1] => 8 [2] => 2 ))

Now I need to output the data into the following format:

$xls->addRow(Array("Car",1,9,0));
$xls->addRow(Array("Truck",2,10,0));
$xls->addRow(Array("Bus",1,8,2));

I tried to do something like this:

foreach($_SESSION['myARRAY'] AS $key => $value) {

    $arr[$key] = $key;

    foreach($value AS $k => $v) {
        $arr[$key] = $v;
    }

    $xls->addRow($arr[$key]);
}

but it did not really work. I think I'm close but not quite there...

  • 写回答

2条回答 默认 最新

  • dongwang3066 2011-06-10 16:37
    关注

    $value is already an array. Now you only need to prepend the $key to it. You could use array_unshift:

    foreach($_SESSION['myARRAY'] AS $key => $value) {
        array_unshift($value, $key);
        $xls->addRow($value);
    }
    

    Of course if you do this more than once, you should consider storing the consolidated array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线