如何从多维数组中删除重复的数组

时间:2018-07-30 14:03:34

标签: php arrays

我想从多维数组中删除重复的数组

以下是我的代码

$getResult = $wpdb->get_results("SELECT *FROM `".$wpdb->prefix."flyusers` WHERE  user_id=$userid ORDER BY id");             

 if(isset($getResult) && !empty($getResult)){
    foreach($getResult as $key=>$val){

        $getStorePosts = get_posts($args);

        $postTitle = array_column($getStorePosts, 'post_title','ID');
        $main = array();
        foreach ($getStorePosts as $key => $value) {
            $id =$value->ID;
            $titlesOfProduct[$id]= $value->post_title;
        }

        echo "<pre>";
        print_r ($titlesOfProduct);
        echo "</pre>";
    }
} 

正在显示以下输出。

<pre>
Array
(
    [665] => Royal Wulff size 8
    [13] => Royal Wulff
)
</pre>

<pre>
Array
(
    [665] => Royal Wulff size 8
    [13] => Royal Wulff
    [128] => Blue Wing Olive (BWO) Nymph
)
</pre>

但是我只想要这个输出

<pre>
Array
(
    [665] => Royal Wulff size 8
    [13] => Royal Wulff
    [128] => Blue Wing Olive (BWO) Nymph
)
</pre>

有人请我帮忙。

1 个答案:

答案 0 :(得分:0)

您正在循环内输出。循环后输出,您只会得到一个完整的数组。

$getResult = $wpdb->get_results("SELECT *FROM `".$wpdb->prefix."flyusers` WHERE  user_id=$userid ORDER BY id");


if(isset($getResult) && !empty($getResult)){
     foreach($getResult as $key=>$val){
          $getStorePosts = get_posts($args);
          $postTitle = array_column($getStorePosts, 'post_title','ID');
          $main = array();
          foreach ($getStorePosts as $key => $value) {
              $id =$value->ID;
              $titlesOfProduct[$id]= $value->post_title;
          }
      }
      echo "<pre>";
      print_r ($titlesOfProduct);
      echo "</pre>";
}