i am trying to store all the pages of a site in a 3 dimensional array. some areas of the site only have 2 levels, others 3. i am trying to check for the existence of a third level in order to pass the right page name to an include. where a 3rd level is not found i want a default page to be shown.

i am using array_key_exists() to check but am not getting the result i am expecting...

<?php

$pages = array (
"news" => array ( '1' => 'latest',
'2' => 'archived',
'3' => array ( '1' => 'photos',
'2' => 'media'
),
),
"about" => array ( '1' => 'history',
'2' => 'strategic',
'3' => array ( '1' => 'management',
'2' => 'board',
'3' => 'contract'
),
),

"services" => array ( '1' => array ( '1' => 'project management case study'),
'2' => array ( '1' => 'track renewals case study'),
'3' => array ( '1' => 'signal installation case study'),

),
);

if (@array_key_exists($pages['about']['3']['1']))
{
echo "array key found";
echo "<br>";
echo $pages['about']['3']['1'];
}

else {
echo "array key does not exist";
echo "<br>";
}

?>

the key clearly exists yet the if loop always returns 'array key does not exist' - what have i done wrong??

ps. i suppressed the following warning message:

Warning: Wrong parameter count for array_key_exists()

    did you try isset() instead?

      yeh, that doesnt work - it throws the opposite problem.. using

      if (isset($pages['about']['2']['1']))
      {
      echo "array key found using isset";
      echo "<br>";
      echo $pages['about']['2']['1'];
      }
      else {
      echo "array key does not exist using isset";
      echo "<br>";
      }

      the key is always found even using the above ['about']['2']['1'] key which clearly doesnt exist - plus bizarrely, the line

      echo $pages['about']['2']['1'];

      outputs the letter 't'

        The error message you suppressed ("wrong parameter count") was issued because the parameter count was wrong. As the manual shows, the function takes two parameters; the first is the key to search for, and the second is the array to search in. So if you want to find if there is a key "1" in the array "$pages['about']['3']" you would do this:

        if (array_key_exists('1', $pages['about']['3']) {

          appreciated installer, was late last night and i did read the manual honest 😃, working now, what about the isset issue ... why does that always return true even though there is no such key?

          thanks for your help btw

            ...why does that always return true even though there is no such key?

            ??

            But there is such a key. "$pages['about'][3][1]" is "management".

            Run this:

            $pages = array('news'    => array(1 => 'latest', 
                                               2 => 'archived', 
                                               3 => array(1 => 'photos', 
                                                          2 => 'media')), 
                           'about'    => array(1 => 'history', 
                                               2 => 'strategic', 
                                               3 => array(1 => 'management', 
                                                          2 => 'board', 
                                                          3 => 'contract')), 
                           'services' => array(1 => array(1 => 'project management case study'), 
                                                          2 => array(1 => 'track renewals case study'), 
                                                          3 => array(1 => 'signal installation case study'))); 
            
            for ($key = 0; $key <= 4; $key++) {
                if (array_key_exists($key, $pages['about']['3'])) { 
                    echo 'Array key "' . $key . '" found:<br>'; 
                    echo '$pages[\'about\'][3][' . $key . '] = "' .  $pages['about']['3'][$key] . '"<br>';
                    echo '<br>'; 
                } else { 
                    echo 'array key "' . $key . '" does not exist<br>'; 
                    echo '<br>'; 
                } 
            }
            

            Displays:

            array key "0" does not exist

            Array key "1" found:
            $pages['about'][3][1] = "management"

            Array key "2" found:
            $pages['about'][3][2] = "board"

            Array key "3" found:
            $pages['about'][3][3] = "contract"

            array key "4" does not exist

              ahh with the isset fn i searched for this key:

              $pages['about']['2']['1'])

              cunningly changing the '3' for a '2' - there isnt a [2][1] it doesnt exist.. so still not sure why it returns true... anyway ive simplified my arrays now so i have a 2d array for the first and second levels and then those values reference a second array for the third level of navigation.. its a bit more manageable, multidimensional arrays fry my brain 🙂

              thanks for your help

                Write a Reply...