if statement within an array

3 posts by 2 authors in: Forums > CMS Builder
Last Post: September 18, 2017   (RSS)

By Mikey - September 18, 2017

Anyone have suggestions on how to get an if statement to work within an array?

Below is a snippet of code showing what I'm trying to accomplish... thought what I have fails to work, but I think you'll get the idea.

    $sectionsToField = array(
        'news'                => 'title',
        'media'                => 'name',
    if ($site_settingsRecord['enable_events'] =='1' ) {
        'events'             => 'title',
    }
        'about'                => 'name',
        'products'            => 'name',
        'services'            => 'name',
    );

Thanks for any suggestions,

Zicky

By Dave - September 18, 2017

Hi Zicky, 

There are a few ways to do that.  The simplest might be to add it afterwards.  How about this:

    $sectionsToField = array(
        'news'                => 'title',
        'media'                => 'name',
        'about'                => 'name',
        'products'            => 'name',
        'services'            => 'name',
    );

    if ($site_settingsRecord['enable_events'] == '1' ) {
        $sectionsToField['events'] = 'title';
    }

Let me know if that works for you!

Dave Edis - Senior Developer

interactivetools.com

By Mikey - September 18, 2017

Thanks Dave - works like a charm!