2

So Im looking for way to convert string into var... I need do get data from array $Map As key I use $URL that get data from real URL and seperate it... $URL = explode("/",$URL); So when I access values of $Map it need to be written like this:

$Map[$URL[0]][$URL[1]][$URL[2]][$URL[3]][$URL[4]][$URL[5]]

So I decide to create function that generate amount of [$URL[0]]'s like

function GenerateURl($i){for ($x = 0; $x <= $i; $x++){
        $URls.='$URL['.$x.']';    
        }
        return($URls);}

Why? I have map of pages that can host visit , thats what $MAP contains, each dimension has style and content setting, but I really dont want to type every single dimension manually + its pain to maintace it...

if($Map[$URL[0]){ #Check if first dimension exist (Index.php/example)
   if....... #this take much more steps to verify if there is request for child, and if child exist, ofc there is else method that redirect to closest parent page
   $Style="style_for_page.css";
   $Contend="style_for_page.css";
}
else{
header("Location: https://example.com");
}

I dont know what I do wrong... But when I call my function GenerateURl(5) output is string... not working var... so its like

$URL[0]$URL[1]$URL[2]$URL[3]$URL[4]

and not like

Profile User Settings Privacy Examle...... (example.com/Profile/User/Settings/Privacy/Examle)

Update: To simplify my question... I need this:

$MAP = array(........) ;
$URL = array("example");
$var = "[$URl[0]]"; #$URL need to be transferred as text to print part
Print($Map[$var]);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • what is a `var`? – Martin Sep 30 '22 at 16:48
  • perhaps you want to [`explode`](https://www.php.net/manual/en/function.explode.php) the URL string and split it on the `/`; `$array = explode('/',$_SERVER['REQUEST_URI']);` – Martin Sep 30 '22 at 16:52
  • no idea what you mean by "working var". Are you talking about an array, perhaps? – ADyson Sep 30 '22 at 16:55
  • ,P.S. It would help a lot if you give an example of the string you're trying to parse, and the exact output you want at the end - your example output currently doesn't really make much sense, I'm afraid. – ADyson Sep 30 '22 at 16:56
  • `when I call my function GenerateURl(5) output is string`...well yes, `.=` adds a string to another string, so that's what you should expect. Are you actually trying to split the URL string into different array indexes? If so, the explode would be likely to help, as mentioned above. – ADyson Sep 30 '22 at 16:58
  • Simply I need put [$URL[0]][$URL[1]] behind $Map... To achieve this : $Map[$URL[0]][$URL[1]]... Amount of "[$URL[1.....99999]]" is Dynamic – Jimmy Lukášík Sep 30 '22 at 17:36
  • Give us a _real_ example of $URL that we could actually use to make a demo – ADyson Sep 30 '22 at 17:53
  • $URL is from browser, it depend on adress for example : if url is www.example.com/profile/setting/test/idknow then $URL=("profile", "settings", "test", "idknow" ) – Jimmy Lukášík Sep 30 '22 at 18:04
  • Ok. So what do you expect the result of `Print($Map[$var]);` to be at the end, in that example? – ADyson Sep 30 '22 at 18:07
  • Well so if adress is : www.example.com/profile then I need to have code: Print($Map[$URL[0]] ) which is same in this example as Print($Map["profile"] )..... If adress is www.example.com/profil/setting. Then I need Print($Map[$URL[0]][$URL[1]])... I would describe it as... Print as many [$URL[0]] as I want... Number must rise by 1..... $Map[$URL[0]][$URL[1]][$URL[2]] amount of "URL" is Dynamic... Sometime I need to print 1 sometimes 10... Output is far complicated... I check if($Map[$URL[0]]) exist... Check if it contain more then 3 childs... Map is 1...10 dimensional array – Jimmy Lukášík Sep 30 '22 at 18:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248489/discussion-between-jimmy-lukasik-and-adyson). – Jimmy Lukášík Sep 30 '22 at 21:02

1 Answers1

1

You can't quite do this in exactly the way you're imagining - PHP can't generate an executable set of array access code (well, you could, and then execute it using eval, but you really don't want to do that unless there's no other option).

A better way is to pass the arguments to a helper function which will then iterate through the target array gradually using each of the arguments provided.

The other task of course is to split the URL string into parts using explode. It looks like you want to ignore the first parts (protocol and domain) and just look at the elements of path.

This code should do what you need, I think:

function flatCall($data_arr, $data_arr_call){
    $current = $data_arr;
    foreach($data_arr_call as $key){
        $current = $current[$key];
    }

    return $current;
}

$MAP = array("profile" => array("setting" => array("test" => array("idknow" => "someValue"))));
$urlString = "www.example.com/profile/setting/test/idknow";

$URL = explode("/", $urlString); //split URL string into an array
array_shift($URL); //remove first element
$result = flatCall($MAP, $URL);
echo $result;

Live demo: https://3v4l.org/A3Z1H


Credit to https://stackoverflow.com/a/36334761/5947043 for the helper function - read that answer for an explanation of how it works.

ADyson
  • 57,178
  • 14
  • 51
  • 63