简体   繁体   English

in_array在函数内部不起作用

[英]in_array Not Working inside Function

An array variable $selected_locations contains a value TH and a string variable $my_location is set to 'TH' . 数组变量$selected_locations包含值TH而字符串变量$my_location设置为'TH'

I verified this using: 我使用以下方法验证了这一点:

<?php
$selected_locations = get_post_meta( $item_id, 'locations', true );
print_r($selected_locations);
$user_location = geoip_detect2_get_info_from_current_ip();
$my_location = $user_location->country->isoCode;
echo $my_location;
?>

However, the line 但是,线

if( in_array($user_country_code, $selected_locations, true ) )
        $visible = false;

in the function below isn't working. 在下面的功能中不起作用。

function visibility_check( $items, $menu, $args ) {
    $user_location = geoip_detect2_get_info_from_current_ip();
    $user_country_code = $user_location->country->isoCode;
    $selected_locations = get_post_meta( $item_id, 'locations', true );
    $hidden_items = array();
    foreach( $items as $key => $item ) {
        $item_parent = get_post_meta( $item->ID, '_menu_item_menu_item_parent', true );
        if( in_array($user_country_code, $selected_locations, true ) )
            $visible = false;
        else
            $visible = true;
        if( ! $visible || isset( $hidden_items[$item_parent] ) ) { // also hide the children of unvisible items
            unset( $items[$key] );
            $hidden_items[$item->ID] = '1';
        }
    }

    return $items;
}

If I assign TH to the array variable $selected_locations manually, it works. 如果我手动将TH分配给数组变量$selected_locations ,它将起作用。

Any suggestions? 有什么建议么?

EDIT: 编辑:

I just realized the array is returning data in the function below Array ( [0] => 'TH' ) , but is returning empty in the function above. 我刚刚意识到数组在Array ( [0] => 'TH' )下面的函数中返回数据,但是在上面的函数中返回空。 Array ( [0] => )

function option( $fields, $item_id ) {
ob_start(); ?>
    <p class="field-visibility description description-wide">
        <label for="edit-menu-item-visibility-<?php echo $item_id; ?>">
            <?php _e('Enter country code(s) separated by commas') ?>:
            <input
            type="text" 
            class="widefat code" 
            id="edit-menu-item-visibility-<?php echo $item_id; ?>" 
            name="menu-item-visibility[<?php echo $item_id; ?>]" 
            value="<?php echo esc_html( get_post_meta( $item_id, 'locations', true ) ); ?>" /></br>
            <input
            type="radio"
            id="edit-menu-item-visibility-<?php echo $item_id;?>"
            name="menu-item-show-hide[<?php echo $item_id; ?>]" 
            value="hide" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'hide', true ); ?>
            />Hide from these locations.</br>
            <input
            type="radio"
            id="edit-menu-item-visibility-<?php echo $item_id; ?>"
            name="menu-item-show-hide[<?php echo $item_id; ?>]"
            value="show" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'show', true ); ?>
            />Only show to these locations.</br>
            <?php
            $locations_string = esc_html( get_post_meta( $item_id, 'locations', true ) ); 
            $locations_array = explode(',', $locations_string);
            $locations_array_trimmed = array_map('trim', $locations_array);
            print_r($locations_array_trimmed);
            $user_location = geoip_detect2_get_info_from_current_ip();
            $user_country_code = $user_location->country->isoCode;
            echo $user_country_code;
            ?>
        </label>
    </p>
<?php
$fields[] = ob_get_clean();
return $fields;
}

I was declaring my variables out of scope. 我在声明变量超出范围。 Rearranging things a bit fixed it. 重新整理一下东西可以解决它。

function visibility_check( $items, $menu, $args ) {
$hidden_items = array();
foreach( $items as $key => $item ) {
    $user_location = geoip_detect2_get_info_from_current_ip();
    $user_country_code = $user_location->country->isoCode;
    $selected_locations = get_post_meta( $item_id, 'locations', true );
    $item_parent = get_post_meta( $item->ID, '_menu_item_menu_item_parent', true );
    if( in_array($user_country_code, $selected_locations, true ) )
        $visible = false;
    else
        $visible = true;
    if( ! $visible || isset( $hidden_items[$item_parent] ) ) { // also hide the children of invisible items
        unset( $items[$key] );
        $hidden_items[$item->ID] = '1';
    }
}

return $items;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM