Mike
Mike

Reputation: 605

PHP Warning: Illegal string offset and Invalid argument supplied for foreach()

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.

Upvotes: 0

Views: 2187

Answers (3)

EvilEpidemic
EvilEpidemic

Reputation: 469

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>';
}

Upvotes: 0

David Jones
David Jones

Reputation: 4305

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) {

Upvotes: 1

tttpapi
tttpapi

Reputation: 887

if (isset($value['subpages']) && is_array($value['subpages'])) {
  foreach ($value['subpages'] as $subfilename => $subpageTitle) 
}

Upvotes: 0

Related Questions