0

I'm struggling with an error using in_array().

In the code below I've got $active_modules as the return of a metabox.io function that gets values from an options page. This return value is an array but in_array() keeps complaining that it's not.

"Warning: in_array() expects parameter 2 to be array, bool given"

I've added a second function right below this in the same file using the same array values that dumps this in an admin notice for testing. I don't understand why this works fine while the first code doesn't? Any pointers in the right direction would be greatly appreciated!

<?php

if ( ! defined( 'WPINC' ) ) {
    die;
}

// Array of modules in plugin to be used for autoloader and settings page.
$all_modules = array(
    array(
        'id'    => 'products',
        'title' => 'Products',
        'path'  => 'kdc-products/kdc-products.php',
    ),
    array(
        'id'    => 'services',
        'title' => 'Services',
        'path'  => 'kdc-services/kdc-services.php',
    ),
    array(
        'id'    => 'locations',
        'title' => 'Locations',
        'path'  => 'kdc-locations/kdc-locations.php',
    ),
);

// This gives an error: Warning: in_array() expects parameter 2 to be array, bool given...
$active_modules = rwmb_meta( 'modules', array( 'object_type' => 'setting' ), 'kdc-site-functions' );

foreach ( $all_modules as $module ) {
    if ( in_array( 'products', $active_modules, true ) ) {
        require plugin_dir_path( __FILE__ ) . $module['path'];
    }
}

/**
 * This function works fine with the same array values as above.
 *
 * @return void
 */
function sample_admin_notice__success() {
    $all_modules    = array(
        array(
            'id'    => 'products',
            'title' => 'Products',
            'path'  => 'kdc-products/kdc-products.php',
        ),
        array(
            'id'    => 'services',
            'title' => 'Services',
            'path'  => 'kdc-services/kdc-services.php',
        ),
        array(
            'id'    => 'locations',
            'title' => 'Locations',
            'path'  => 'kdc-locations/kdc-locations.php',
        ),
    );
    $active_modules = rwmb_meta( 'modules', array( 'object_type' => 'setting' ), 'kdc-site-functions' );
    foreach ( $all_modules as $module ) {
        if ( in_array( 'products', $active_modules, true ) ) {
            $my_array[] = $module['id'];
        }
    }
    ?>
    <div class="notice">
        <p>
            <?php
                print_r( $my_array );
            ?>
        </p>
    </div>
    <?php
}
add_action( 'admin_notices', 'sample_admin_notice__success' );
| improve this question | | | | |
0

Welcome to WPSE. I think I see your issue, but without access to the output of rwmb_meta( 'modules', array( 'object_type' => 'setting' ), 'kdc-site-functions' ), I cannot be sure.

Your problem appears to be that you are looping through $all_modules, but using in_array() to examine $active_modules. Based on your loop, here is what I would expect to read:

// Because you are looping through $all_modules for $module, I assume you should use it...
foreach ( $all_modules as $module ) {
    // If I examine $module, this should not throw and error
    if ( in_array( 'products', $module, true ) ) {
        require plugin_dir_path( __FILE__ ) . $module['path'];
    }
}

Looking at your current code, I would assume that you are getting a failure (FALSE) value back from rwmb_meta(), then popping that into your in_array() function causes your error.

Otherwise, I would suggest you at least examine the results of your rwmb_meta() function, to ensure it is an array:

if (is_array($active_modules) && !empty($active_modules)){
    // other code here...
}

Hope this solves it! Good luck.

| improve this answer | | | | |
  • Yep, you nailed it. I'm still learning WordPress hooks and didn't realize rwmb functionality wasn't available yet. The plugin author got back with the hook location and I got it sorted. Thanks – Wade Koehn Oct 2 '19 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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