从for循环中的数组中删除重复的id

时间:2014-01-29 09:45:00

标签: php

我在产品上有不同的参数......但是我想只显示那些被用户点击的参数。我已经这样做了但我想删除重复...我的代码是: - < / p>

$ab=$_POST['data1'];
$karan=explode(",",$ab);
array_pop($karan);

for($i=0;$i<count($karan);$i++){
    'karan['.$i.'] = '.$karan[$i];

    $posts = $wpdb->get_results("SELECT * FROM demo_postmeta WHERE meta_value='".$karan[$i]."' ");

    $out=array();
    foreach($posts as $post){       


        if (!isset($out[$post->post_id])){

            $out[$post->post_id][$post->meta_key]=$out;



            $ram=get_the_post_thumbnail($post->post_id,"_thumbnail_id",true);
            $title=get_post_meta($post->post_id,"_product_title",true);
            $price=get_post_meta($post->post_id,"_product_year",true);
            $link=get_post_meta($post->post_id,"_product_likes",true);
?>      



<div id="itemListContent" class="in-list">
    <div class="the-list">
        <div class="li first row clearfix" style="cursor: pointer;">
            <div class="c-1 table-cell">
                <div class="cropit">
                    <a class="pics-lnk important-rule" href="http://uzodocs.com/PROJECTS/demo/products?post_id=<?php echo $post->post_id ?>" style="text-decoration: none;">
                        <?php echo $ram; ?> 
                    </a>
                </div>
            </div>
            <div class="second-column-container  table-cell">
                <h3>
                    <a href="http://uzodocs.com/PROJECTS/demo/products?post_id=<?php echo $post->post_id ?>" title="Kenxinda Watch Mobile Dual SIM with FREE Bluetooth Headset free home delivery - Delhi" class="important-rule" style="text-decoration: none;">
                        <?php echo $title; ?>
                    </a>
                </h3>
                <div class="c-4">
                </div>
                <span class="itemlistinginfo clearfix">
                    <span>
                        Delhi | 
                        Cameras
                        - Camera Accessories
                    </span>
                    <span>
                    </span>
                </span>
            </div>
            <div class="third-column-container table-cell">

                <?php echo $price; ?>

            </div>

            <div class="fourth-column-container table-cell"><br>  </div>
        </div>
        <div class="li even row clearfix" style="cursor: pointer;">
            <div class="c-1 table-cell">
            </div>

        </div>
    </div>
</div>

<?php       

        }
        $out[$post->post_id][$post->meta_key][] = $post->post_id;


    }


}
die;

?>

2 个答案:

答案 0 :(得分:0)

<强>更新 这可能是解决问题的一种方法:

if (!in_array($post->post_id, $out)){

        $out[] = $post->post_id;

您可以移除上一个$out[$post->post_id][$post->meta_key][] = $post->post_id;

答案 1 :(得分:0)

只需将您需要的数据输入     $ out [$ post-&gt; post_id] [$ post-&gt; meta_key] [] =“您需要的数据”。 并将此数组放在范围之外。就在死之前;