There is an array:

$array = ['key1'=>['key2'=>'val']]; 

In the line through the dot are the keys of the array:

 $keys_string = 'key1.key2'; 

It is necessary to extract the value of the array using the keys specified through a dot in the $ keys_string variable.

  • You obviously have something wrong with the data if such a question arises. - Naumov
  • 2
    just a week ago asked a similar question, bots or what? - Rochfort
  • The data is OK) In Laravel, filters use something similar, when the key indicates the key through the dot when checking incoming data. - Konstantin
  • Rochfort, if you send the link I will be grateful. - Konstantin

2 answers 2

 $keys = explode('.',$keys_string ); $value=$array; foreach($keys as $key){ $value = $value[$key]; } var_dump($value); 
  • Boris run, thank you very much! - Konstantin

You can make unlimited with 2 keys depth:

 $array = ['key1' => ['key2' => ['key3' => 'val']]]; $keys_string = 'key1.key2.key3'; var_dump($array); function change(&$arr, $key, $value){ $link = &$arr; foreach(explode('.', $key) as $part){ if(!is_array($link) || !array_key_exists($part, $link)) throw new Exception('Invalid path: ' . $keys_string); $link = &$link[$part]; } $link = $value; } change($array, $keys_string, 'newValue'); var_dump($array); 

https://repl.it/FP5p/0

  • two reasons 1. confused question 2. Well, if brackets are forgotten :) - Naumov
  • Thank you so much for the answer! Please transfer it to the topic with a change in the value of ru.stackoverflow.com/questions/617303/… - Konstantin