0

I'm getting these two php warnings

PHP Warning:  Illegal string offset 'subpages'

and

PHP Warning:  Invalid argument supplied for foreach() in

The line of code is

foreach ($value ['subpages'] as $subfilename => $subpageTitle) {

and here is the full php so you can see what's going on

Array

// Menu Items
$li_1 = 'Temp Jobs';
$li_2 = 'Domestic Jobs';
$li_3 = 'HR';
$li_4 = 'Job Resume Tips';

$pages = array(

    // Temp Jobs
    'temp or temporary or seasonal or holiday or part-time or pt' => $li_1,

    // Domestic Jobs (with submenu)
    $li_2 => array('pageTitle' => $li_2, 'subpages' => array(
        'Baby-Sitter or Babysitter' => 'Babysitter',
        'Nanny' => 'Nanny',
        'Room-Attendant' => 'Room Attendant',
        'Butler or Houseman' => 'Butler',
        'Chauffeur or Chauffeuse' => 'Chauffeur',
        'Maid' => 'Maid',
        'Housekeeper or House-Keeper' => 'Housekeeper',
        'Estate-Manager' => 'Estate Manager',
        'Property-Manager' => 'Property Manager',
        'House-Manager' => 'House Manager',
        'Tutor' => 'Tutor',
        'Caregiver or Nursing-Assistant or CNA' => 'Caregiver')),

    // HR
    $li_3.' or Human-Resource or Human-Resources' => $li_3,

    // Job Resume Tips
    'job-resume-tips' => $li_4
);

foreach ($pages as $filename => $value) {

$lis = "";
$hasCurrent = false;

if (is_array ($value)) {
    $href = '#menu';
    $pageTitle = $value ['pageTitle'];
} elseif (str_replace("-"," ", $filename) == strtolower($value)) {
    $href = $dir_structure.$filename.'/';
    $pageTitle = $value;
} else {
    $href = $dir_structure.'jobs/?position='.urlencode($filename);
    $pageTitle = $value;
}

if ($value  != '') {
    foreach ($value ['subpages'] as $subfilename => $subpageTitle) {
        $lis .= '
                <li'.(($position == $subfilename) ? ' class="current"' : '').'><a href="'.$dir_structure.'jobs/?position='.urlencode($subfilename).'">'.$subpageTitle.'</a></li>';
            if ($position == $subfilename) {
                $hasCurrent = true;
            }
    } // foreach sub_menus
    echo '
        <li'.($hasCurrent || $value == $currentPage || $position == $filename ? ' class="current"' : '').'>
        <a href="'.$href.'">'.$pageTitle.'</a>';
    if($lis != '') {
        echo '
            <ul class="subMenu">';
        echo $lis;
        echo '
            </ul>';
    } // if lis
} // if sub_menus
echo '
        </li>';
} // foreach pages

I already read the other questions on here and know if I replace if ($value != '') { with if (is_array ($value)) { will get rid of the error, but then the navigation menu only shows the tabs that have sub menus. How can I get rid of this php warning and at the same time make sure all of my navigation menu tabs show up? Can't figure it out.

Not all of my top navigation tabs have drop downs. In the example above, only the top tab Domestic Jobs has the drop downs submenu.

Mike
  • 607
  • 8
  • 30

3 Answers3

1

In the first iteration of the loop you are trying get the subpages index of a string.

You should also do a is_array check before the loop. So this could work.

if isset($value['subpages']) && (is_array($value['subpages'])) {
    foreach ($value ['subpages'] as $subfilename => $subpageTitle) {
David Jones
  • 4,275
  • 6
  • 27
  • 51
  • already said in my question above about this. It is below the code. Anyway, if I use your suggestion, I still get this error `PHP Warning: Illegal string offset 'subpages'` – Mike Sep 17 '14 at 09:08
  • Sorry missed out the isset, I have updated my answer – David Jones Sep 17 '14 at 09:14
  • great! no warning, but still only 1 menu tab shows. – Mike Sep 17 '14 at 09:20
  • Not to sure your code looks like it should work, try var_dumping out the $lis in the loop to see what is happening. – David Jones Sep 17 '14 at 09:30
  • just a dumb mistake on my part. I didn't put an else statement below the foreach to echo out the other tabs that don't have drop downs. EvilEpidemic figured that out, so accepted his answer. – Mike Sep 17 '14 at 09:42
  • but just to clarify your answer, why the two if statements? In other words, why also include the isset? – Mike Sep 17 '14 at 09:44
  • You need to check that the key exists and is an array otherwise it will throw a warning because you are trying to check an array key that doesn't exist – David Jones Sep 17 '14 at 10:08
  • If that is the case, then wouldn't the `array_key_exists` be better to use? Like this `if (is_array $value['subpages'] && array_key_exists('subpages', $value))` – Mike Sep 17 '14 at 17:48
  • Same result would be given see this for more info - http://stackoverflow.com/questions/3210935/difference-between-isset-and-array-key-exists – David Jones Sep 17 '14 at 20:50
0
if (isset($value['subpages']) && is_array($value['subpages'])) {
  foreach ($value['subpages'] as $subfilename => $subpageTitle) 
}
tttpapi
  • 887
  • 2
  • 9
  • 32
  • don't get error but only 1 menu tab shows up. It is the one with the drop downs. The other 3 are missing. – Mike Sep 17 '14 at 09:08
0

This seems to achieve the desired effect;

if(is_array($value)) {
    foreach ($value ['subpages'] as $subfilename => $subpageTitle) {
    .....
    } // if lis
} // if sub_menus
else {
    echo '<li class="current"><a href="'.$href.'">'.$pageTitle.'</a>';
}
EvilEpidemic
  • 479
  • 8
  • 17
  • you caught the same edit I did. I placed it down below the if sub_menus, then saw your edit. Warning gone and now all menu items show up as normal. – Mike Sep 17 '14 at 09:40