doucang8303 2017-05-22 12:12
浏览 42

数组对象映射(改进)

I trying to mapping array object into table. actually, my code is working, but maybe is heavy to execute (used 3 foreach), can this be shortened or improved?

My Data (store in $data)

Array
(
    [0] => stdClass Object
        (
            [code] => jne
            [name] => Jalur Nugraha Ekakurir (JNE)
            [costs] => Array
                (
                    [0] => stdClass Object
                        (
                            [service] => CTC
                            [description] => JNE City Courier
                            [cost] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [value] => 17000
                                            [etd] => 2-3
                                            [note] => 
                                        )
                                )
                        )
                    [1] => stdClass Object
                        (
                            [service] => CTCOKE
                            [description] => JNE City Courier
                            [cost] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [value] => 15000
                                            [etd] => 3-4
                                            [note] => 
                                        )
                                )
                        )
                )
        )
    [1] => stdClass Object
        (
            [code] => J&T
            [name] => J&T Express
            [costs] => Array
                (
                    [0] => stdClass Object
                        (
                            [service] => EZ
                            [description] => Regular Service
                            [cost] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [value] => 20000
                                            [etd] => 
                                            [note] => 
                                        )
                                )
                        )
                )
        )
)

My Code

foreach ($data as $data) {
            $h = '<tr><th colspan="3">'.$data->{'name'}.'</th></tr>';
            $s = "";
            foreach ($data->{'costs'} as $subdata) {
              foreach ($subdata->{'cost'} as $ev) {
                $s .= '<tr><td>'.$subdata->{'service'}.' <small class="text-muted"><em>'.$subdata->{'description'}.'</em></small></td><td style="width:100px;" class="text-center">'.$ev->{'etd'}.' Days</td><td style="width:120px;"><span class="pull-left">Rp.</span><span class="pull-right">'.$ev->{'value'}.'</span></td></tr>';
              }
            }
            $buildHTML .= $h.$s;
          }

echo '<table class="table table-bordered table-hover"><tbody>'.$buildHTML.'</tbody></table>';

Thanks for your helping and Sorry for my bad english.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 切换TabTip键盘的输入法
    • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥20 使用Photon PUN2解决游戏得分同步的问题
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序