0

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.

4
  • 2
    looks like a private property, does this object have a getter method? Commented Mar 24, 2015 at 10:01
  • Not that I know of, I'm able to get the values with what I've tried, just wondering if there is a better way of doing it? Commented Mar 24, 2015 at 10:08
  • you wrote $v['value'] but the print_r states [values] Commented Mar 24, 2015 at 10:16
  • @RobMorris oh okay, odd, anyway, what do you mean better way? like a more shorter approach? Commented Mar 24, 2015 at 10:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.