首页 > 解决方案 > in_array 语句的问题

问题描述

我想在产品页面上仅针对某些产品和某些国家/地区显示某些 URL 链接。最初,我检索国家/地区的 IP,然后检查产​​品 ID。如果产品 ID 在数组中,如果用户来自加拿大,则显示链接 1,如果用户来自美国,则显示链接 2。问题是它没有显示任何东西。包含 in_array 的 if 语句中没有任何反应,并且不确定我做错了什么。

<?php
if ($currentCountry =="CA" || $currentCountry =="US") 
{
    $p_id = $_product->getId();   
    $include_id = array(546, 125,135);
    ?>
    <style>
    .choose-local {
        display: flex;
    }
    .data-section {
        min-width: 33%;
    }
    .choose-local .data-section span {
        float: left;
    }
    </style>
    <?php
    if (in_array($p_id,$include_id, TRUE)) 
    {

        ?>
        <div class='choose-local'>
            <div class='data-section'>
                <span style="">Available Locally from :</span>
            </div>
            <div class="data-section">
                <?php 
                    if ($currentCountry =="CA") 
                    { 
                        ?>
                        <span>Link 1<a href='https://www.link1.com'>[Order from here]</a></span>
                        <?php
                    }

                    if ($currentCountry =="US") 
                    { 
                        ?>        
                        <span>Link 2<a href='https://www.link2.com'>[Order from here]</a></span>
                        <?php 
                    } 
                ?>      
            </div>
        </div>
    <?php 
    }
}
?>

标签: phparrays

解决方案


检查天气你的 $p_id 是字符串还是整数

<?php
                                $currentCountry = "CA";
if ($currentCountry =="CA" || $currentCountry =="US") 
{
    $p_id = 125;   
    $include_id = array(546, 125,135);
    ?>
    <style>
    .choose-local {
        display: flex;
    }
    .data-section {
        min-width: 33%;
    }
    .choose-local .data-section span {
        float: left;
    }
    </style>
    <?php
    if (in_array($p_id,$include_id, TRUE)) 
    {

        ?>
        <div class='choose-local'>
            <div class='data-section'>
                <span style="">Available Locally from :</span>
            </div>
            <div class="data-section">
                <?php 
                    if ($currentCountry =="CA") 
                    { 
                        ?>
                        <span>Link 1<a href='https://www.link1.com'>[Order from here]</a></span>
                        <?php
                    }

                    if ($currentCountry =="US") 
                    { 
                        ?>        
                        <span>Link 2<a href='https://www.link2.com'>[Order from here]</a></span>
                        <?php 
                    } 
                ?>      
            </div>
        </div>
    <?php 
    }
}
?>


推荐阅读