dongyuan1983 2015-03-24 10:00
浏览 32

从多维数组php返回值

I'm hoping there is a better way of returning the values from each of cy_GB['value] and en_GB['value] from the array below:

MultilingualSelectAttributeTypeOptionList Object (
    [options:MultilingualSelectAttributeTypeOptionList:private] => Array
        (
            [0] => MultilingualSelectAttributeTypeOption Object
                (
                    [error] => 
                    [id] => 7
                    [values] => Array
                        (
                            [cy_GB] => Array
                                (
                                    [id] => 13
                                    [value] => Audio described
                                )

                            [en_GB] => Array
                                (
                                    [id] => 14
                                    [value] => Audio described
                                )

                        )

                    [th] => TextHelper Object
                        (
                        )

                )

            [1] => MultilingualSelectAttributeTypeOption Object
                (
                    [error] => 
                    [id] => 3
                    [values] => Array
                        (
                            [cy_GB] => Array
                                (
                                    [id] => 5
                                    [value] => BSL signed
                                )

                            [en_GB] => Array
                                (
                                    [id] => 6
                                    [value] => BSL signed
                                )

                        )

                    [th] => TextHelper Object
                        (
                        )

                )

        )

    [error] => 
)

This is what I've tried. I should also use more meaningful names.:

foreach ($selectedOptions as $row) {

    foreach ($row as $key) {

        foreach ($key as $k => $v) {

            if($k == 'cy_GB') {
                echo $v['value'];
            }

            if($k == 'en_GB') {
                echo $v['value'];
            }

        }

    }

}

I know this kind of thing has been asked many times before so I apologise for that. any help would be most appreciated.

  • 写回答

1条回答 默认 最新

  • douxuanyi2813 2015-03-24 10:22
    关注

    Something like this could work:

    function findKeyRec($obj, $search) {
    
      if( !is_array( $obj ) && !$obj instanceof Traversable ) return;
    
      foreach($obj as $key => $value) {
        if($key == $search) {
            echo $value['value'];
        } else {
           findKeyRec($value, $search);
        }
      }
    }
    
    findKeyRec($ar, 'cy_GB');
    findKeyRec($ar, 'en_GB');
    

    It's not shorter, but in my opinion more elegant, and it should work with any object/array structure.

    Untested.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题