1

I am trying to pass a dynamic variable into a multidimensional array.

This is the actual code:

for($i = 0; $i < count($social ["#object"] -> field_sector ["und"]); $i++){
    echo $social ["#object"] -> field_sector ["und"] [$i] ["taxonomy_term"] -> name;
}

Since I want to re-use this code for multiple types, I created a function

function render_multi_array ($parent, $field_name) {

  for($i = 0; $i < count($parent ["#object"] -> $field_name ["und"]); $i++){
        echo $parent ["#object"] -> $field_name ["und"] [$i] ["taxonomy_term"] -> name;
    }
}

The issue is happening with $field_name as I am unable to provide this dynamically. Any idea how I can make this function work?

Sample array is follows:

Array
(
    [#title] => Sector
    [#field_name] => field_sector
    [#object] => stdClass Object
        (
            [vid] => 1079
            [uid] => 30
            [vuuid] => 83ab0817-0175-4541-b20e-93611c20c026
            [nid] => 1077
            [type] => random_study
            [field_random_id] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [value] => CS_525
                                    [format] => 
                                    [safe_value] => CS_525
                                )

                        )

                )
[field_sector] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [tid] => 411
                                    [taxonomy_term] => stdClass Object
                                        (
                                            [tid] => 411
                                            [vid] => 10
                                            [name] => Sample title goes here.

                                        )

                                )

                            [1] => Array
                                (
                                    [tid] => 248
                                    [taxonomy_term] => stdClass Object
                                        (
                                            [tid] => 248
                                            [vid] => 10
                                            [name] => Energy


                                        )

                                )

                        )

                )
2
  • It does work for me. What is your PHP version? Commented Dec 5, 2019 at 1:08
  • PHP version 5.5 Commented Dec 5, 2019 at 1:21

1 Answer 1

2

In PHP 7 the code should work as written. PHP 7.0 changed evaluation order to strictly left to right.

In PHP 5, you'll get an illegal string offset warning because PHP is trying to first evaluate $field_name["und"] (e.g. "field_sector"["und"]) and use the result of that as the property name in #object rather than evaluating $parent["#object"]->$field_name to get the array and then accessing that with ["und"].

You can prevent that by bracketing the variable like this:

function render_multi_array ($parent, $field_name) {
    for ($i = 0; $i < count($parent["#object"]->{$field_name}["und"]); $i++){
        echo $parent ["#object"]->{$field_name}["und"][$i]["taxonomy_term"]->name;
    }
}

Adding those brackets won't cause any problems if you upgrade to PHP 7 later.

By the way, it looks like this code would be simpler with a foreach loop instead.

function render_multi_array ($parent, $field_name) {
    foreach ($parent['#object']->{$field_name}['und'] as $item) {
        echo $item['taxonomy_term']->name;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant! Perfect solution.
"1+" for foreach idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.