簡體   English   中英

in_array在函數內部不起作用

[英]in_array Not Working inside Function

數組變量$selected_locations包含值TH而字符串變量$my_location設置為'TH'

我使用以下方法驗證了這一點:

<?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;
?>

但是,線

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

在下面的功能中不起作用。

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

如果我手動將TH分配給數組變量$selected_locations ,它將起作用。

有什么建議么?

編輯:

我剛剛意識到數組在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;
}

我在聲明變量超出范圍。 重新整理一下東西可以解決它。

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