0

I'm going to try to make this as simple as possible. A dot notation string gets sent to a function, so for example, this would be the string.

'user.id' 

Then I explode the dot to give me an array of names

$dotNotation = explode(".", $variable);

And now I need to use these names to select from another variable like so

$row[$dotNotation[0]][$dotNotation[1]]

So this would be

$row['user']['id']

However, I need it to be dynamic, it can be any depth i.e there can be any number of dots. I'm totally stuck on how to achieve this.

1

1 Answer 1

0

You could just iterate through all the keys and retrieve the value:

function getDotNotationValue($dotNotation, $row) {
   $dotNotationArray = explode(".", $dotNotation);
   foreach($dotNotationArray as $key) {
      $row = $row[$key];
   }
   return $row;
}

$row = ["test1" => ["test2" => ["test3" => "value"]]];
$string = "test1.test2.test3";
echo getDotNotationValue($string, $row);
Sign up to request clarification or add additional context in comments.

Comments

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.