I have a php page that gets a value from $_GET and according to this value selects different values in a multidimensional array
$data =
array(
"index" => array(
"name" => "title",
"title" => "<img src = 'logo.png' alt=''>",
"fallback_html" => "main.php",
"gallery" => array("1" => "nothing")),
"gallery1" => array(
"name" => "gallery1",
"title" => "Gallery 1",
"fallback_html" => "",
"gallery" => array("1" => "jpg1.jpg","2" => "jpg2.jpg")
)
);
here is the code
if(isset($_GET['p'])){
$page = $_GET['p'];
}
else {
$page = "index";
}
echo $data[$page]['title'];
and i am getting a output like that
Notice: Undefined index: 'index' in D:\xampp\htdocs\egliphp\index.php on line 66
if i change the $page value to ‘index’ or ‘gallery1’ it works just fine
The logic seems sound, so it could be a whitespace & non-alphanumeric character issue. So I recommend doing this:
I also added an
array_key_existsso the logic can handle a request that is being made to a non-existant page or array index.EDIT: Also, your array data is inconsistent. Dong an
echo $data['index']['title'];will result in:But doing an
echo $data['gallery1']['title'];will result in:So I think you need to iron that out as well.
EDIT FOR ADDITIONAL INFO ON THE MIXED ARRAY VALUE STUFF: Okay, from a programming standpoint there is still an issue with the way the array has mixed value types for the value
$data['title']. I see that as a problem waiting to happen. So I would recommend reworking the $data array structure as so:Specifically I did this to the
$data["index"]["title"]that has an image tag:And this to the
$data["gallery1"]["title"]that has a text string:That will help you on the rendering side of things better handle each case. So you could have code as follows:
The example above shows that if you tag the type at the datasource, you can do different things to each type when it comes time to render. If you feel it’s too much work, that’s fair. But mixing datatypes in values is never a good idea without someway to clearly differentiate them.