I am using a multidimensional array to hold the variables for a given page. I am trying to get a string from the url and match it with an array within my template array to pull the correct variables to display on the page.

Here is my array:

$template = array(

    "index" => array(
        "title" => "Dashboard",
        "model" => "model/dash.php"
    ),
    "input" => array(
        "title" => "Dashboard",
        "model" => "model/input.php"
    ),
    "jobboard" => array(
        "title" => "Job Board",
        "model" => "model/job_board.php"
    ),
    "jobcreate" => array(
        "title" => "Job Creator",
        "model" => "model/job_create.php"
    )
);

And here is what I am using to try and verify the pages:

if(isset($_GET['page'])){ $page = $_GET['page']; }

if(in_array($page, $template)){
    $title = $template[$page]['title'];
    $model = $template[$page]['model'];
    echo "yes";
}else{
    $title = $template['index']['title'];
    $model = $template['index']['model'];
    echo "no";
}

The echo "yes/no"; is what I am using to debug if it is working or not but no matter what I have done it just keeps on outputting no.

share|improve this question

2 Answers

up vote 0 down vote accepted

Take a look at the documentation of php's in_array()

in_array — Checks if a value exists in an array

It looks like your intention is to check against the index of the array, rather than the value. The values in the array are arrays.

Try using array_key_exists() instead.

if (array_key_exists($page, $template)) {
  $title = $template[$page]['title'];
  $model = $template[$page]['model'];
  echo "yes";
}
else {
  $title = $template['index']['title'];
  $model = $template['index']['model'];
  echo "no";
}
share|improve this answer
Wow your a life saver, I have seriously been stressing on this one for over an hr, I saw the array_key_exists function but honestly due to my inexperience it confused me so i didn't even try it. It worked perfectly and thanks for the explanation. – user2204006 1 hour ago

in_array() looks at values. It is probably keys you're after.

You can check that with array_key_exists().

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.