douchenchepan6465 2014-08-22 18:58
浏览 38
已采纳

具有许多子数组的多维数组? 如何在值[B]中使用foreach来打印前端的值

Using Webservices I receive a reply in the following format:

Array
(
[Data] => Array
    (
        [A] => Array
            (
                [B] => Array
                    (
                        [0] => Array
                            (
                                [C] => Array
                                    (
                                        [value] => some value1
                                    )
                                [D] => Array
                                    (
                                        [value] => some value2
                                    )
                                [E] => some value3
                            )
                        [1] => Array
                            (
                                [C] => Array
                                    (
                                        [value] => some value4
                                    )
                                [D] => Array
                                    (
                                        [value] => some value5
                                    )
                                [E] => 5
                            )
                    )
                [value] => 

using magento as php framework i create a controller displaying the following information

    public function bynumberAction(){
        $t = new \RocketShipIt\Track('fedex');
        $response = $t->track('770190256519');

//        parse results
        $events['C'] = $response['A']['B']['C'];
        $events['D'] = $response['A']['B']['D']
        $events['E'] = $response['A']['B']['E']

//        serve results
        $this->loadLayout( array('default','shipping_track_bynumber'));
        $this->_initLayoutMessages('customer/session');
        $this->getLayout()->getBlock('track_bynumber')
                ->setCollection($events)
                ->setTemplate('shipping/track/bynumber.phtml');
        $this->renderLayout();
    }

On the frontend phtml (bynumber.phtml) file I put:

<?php $res = $this->getCollection() ?>

<?php foreach ($res as $row) {echo $row['C'] . ' ' . $row['D'] . ' ' . $row['E'] . '<br />';} ?>

No information is displayed.. any help appreciated. brgds

  • 写回答

2条回答 默认 最新

  • duanao2585 2014-08-22 19:17
    关注

    There are more issues. First of all, there is a syntax error - missing semicolon after a command on lines 6 and 7 of function bynumberAction.

    Second: why do you have different array structure in your controller, if you know how the response looks?

    You use: $response['A']['B']['C'];

    Response looks: $response['Data']['A']['B']['0']['C'];

    Try this in your controller:

    $events = array()
    foreach ($response['Data']['A']['B'] as $data)
    {
        $events[] = $data;
    }
    

    ...and in a template:

    foreach ($res as $row) { echo $row['C']['value'] ... }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决