dtgv52982 2013-02-19 11:39
浏览 208
已采纳

PHP解包数组

I would like to learn a smart way to unpack nested array. For instance, i have an array variable $rma_data['status'] which looks like below;

 [status] => Array
    (
        [0] => Array
            (
                [created] => 1233062304107
                [statusId] => 5
                [statusName] => Open
            )

        [1] => Array
            (
                [created] => 1233061910603
                [statusId] => 2
                [statusName] => New
            )

        [2] => Array
            (
                [created] => 1233061910603
                [statusId] => 1
                [statusName] => Created
            )

    )

I would like to store the Created timestamps and statusId into an variables based on the condition like: if we find out there is "Open" status exist, we will use Open instead of "New" and "Created" . If there is only New and Created, we will use New instead .

Current version of my way to do that:

for($i=0; $i<count($rma_data['status']); $i++)
                {
                    switch($rma_data['status'][$i]['statusId'])
                    {
                        case 5: 

                                            case 2:

                                            case 3:
                }

Any ideas?

  • 写回答

4条回答 默认 最新

  • douxiuyu2028 2013-02-19 11:48
    关注

    For small to medium scale, what you already have looks good.

    My only suggestions would be to use additional variables, for example the count and to unroll some of the compact code to be more efficient and readable.

    For example:

    $total=count($rma_data['status']);
    for($i=0; $i<$total; $i++){
        $x=$rma_data['status'][$i];
        if($x['statusName']=='Open'){ // Use your criteria
            $t=$x['created'];
            //...Do Work
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错