I need a string to use it for a array pattern to find a value by this. for example
$test = ['test','test2' => ['test3','test4' => ['test5']]];
$pattern = "['test2']['test4']"
$response = $test{$pattern} <- search
give it a way to solve this ?
I need a string to use it for a array pattern to find a value by this. for example
$test = ['test','test2' => ['test3','test4' => ['test5']]];
$pattern = "['test2']['test4']"
$response = $test{$pattern} <- search
give it a way to solve this ?
Based on another question: Using a string path to set nested array data
function GetValueFromPattern($arr, $pattern) {
$exploded = explode(".",$pattern);
$temp = $arr;
foreach($exploded as $key) {
if(key_exists($key, $temp)) {
$temp = $temp[$key];
} else {
return ["status" => false];
}
}
return ["status" => true, "response" => $temp];
}
$test = ['test','test2' => ['test3'=>"a",'test4' => ['test5']]];
$pattern = "test2.test3";
$response = GetValueFromPattern($test, $pattern);
if ($response["status"]) {
echo $response["response"];
} else {
echo "Error!";
}