Jaime Enns
Jaime Enns

Reputation: 1

Get a value from an associative array with $_GET

Say for example that I have this variable

$p0001 = array("title"=>"This is the title","name"=>"Just Me");

And the URL is

https://www.example.com?id=p0001

How do I get the correct array from PHP? I've tried

echo $_GET["id"]["title"];

To explain little more, say I have two array variables. I want it to echo the "title" from from the array $p0001. So how do I make sure I gets the variable I put in $_GET?

Upvotes: 0

Views: 35

Answers (1)

Nemoden
Nemoden

Reputation: 9056

You can use Variable variables

So, your variable name is $p0001. Your GET parameter id is basically a pointer to the variable name, so using variable variables, we can reference the variable we're looking for:

$varname = $_GET['id']; // $varname = 'p0001';
$$varname; // this is basically $p0001
$$varname['title']; // and you can get your title from $p0001

You could also check if a variable exists before using with isset($$varname)

Upvotes: 1

Related Questions