1

I have something like this:

function print_element($array, $field){
  return "Element: {$array[$field]}";
}

$array['name_en'] = 'English name';
echo print_element($array, 'name_en');

I wish to access a property within an array that belongs to the main array like this:

$array['english_values']['name_en'] = 'English name';
echo print_element($array, "['english_values']['name_en']");

Is there a way to accomplish this?

Thx in advance.

1
  • 1
    If your function doesn't return anything, you don't need to call echo before it. Your function handles the echo internally. Commented Apr 3, 2012 at 22:03

3 Answers 3

2
echo print_element($array['english_values'], 'name_en');
Sign up to request clarification or add additional context in comments.

Comments

1

Pass just the string 'english_values,name_en' to your function. Inside the function, explode the string on the comma, then loop through the array and assign $array = $array[$thisKey] on each pass. You may also wish to check that it is_array($array) on each pass.

Comments

0

You have the array and also the keys try this:

    function print_var($val) {
        echo "VAR: {$val} <br/>";
    }

    $array['english_values']['name_en'] = 'English name';
    print_var($array['english_values']['name_en']);

    // OUTPUT
    // VAR: English name

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.