获取特定帖子的独特视图

时间:2016-12-07 17:28:31

标签: php mysql arrays wordpress

我需要显示特定页面的每月唯一视图数。所以为了达到我在下面所做的那样:

$fullArray = array();
$productIds = '100,200,210,220,255'; /* these are specific product ids */
$idsArray = explode(',',$productIds);
foreach ($idsArray as $unique_id_item) {
    $currentUniqueViews = $wpdb->get_results("
        SELECT DISTINCT IP as all_uniques, DATE_FORMAT( insertion_date, '%b' ) as 
        month_name, DATE_FORMAT( insertion_date, '%Y-%M' ) as full_date FROM
        daily_unique_views WHERE page_id = '".$unique_id_item."'
    ");
    if($currentUniqueViews){
        $fullArray[] = $currentUniqueViews;
    }
}
$encodedArray = json_decode(json_encode($fullArray), True);
var_dump($encodedArray);

以上返回:

array(5) {
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Dec"
      ["full_date"]=>
      string(13) "2016-December"
    }
  }
  [1]=>
  array(4) {
    [0]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Oct"
      ["full_date"]=>
      string(12) "2016-October"
    }
    [1]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Nov"
      ["full_date"]=>
      string(13) "2016-November"
    }
    [2]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "122.12.12.122"
      ["month_name"]=>
      string(3) "Dec"
      ["full_date"]=>
      string(13) "2016-December"
    }
    [3]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Dec"
      ["full_date"]=>
      string(13) "2016-December"
    }
  }
  [2]=>
  array(2) {
    [0]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Nov"
      ["full_date"]=>
      string(13) "2016-November"
    }
    [1]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Dec"
      ["full_date"]=>
      string(13) "2016-December"
    }
  }
  [3]=>
  array(3) {
    [0]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Nov"
      ["full_date"]=>
      string(13) "2016-November"
    }
    [1]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Dec"
      ["full_date"]=>
      string(13) "2016-December"
    }
    [2]=>
    array(3) {
      ["all_uniques"]=>
      string(12) "71.111.1.111"
      ["month_name"]=>
      string(3) "Dec"
      ["full_date"]=>
      string(13) "2016-December"
    }
  }
  [4]=>
  array(1) {
    [0]=>
    array(3) {
      ["all_uniques"]=>
      string(13) "111.11.11.111"
      ["month_name"]=>
      string(3) "Dec"
      ["full_date"]=>
      string(13) "2016-December"
    }
  }
}

您如何注意,数组包含重复的数组。例如,下面的数组重复5次。

array(3) {
    ["all_uniques"]=>string(13) "111.11.11.111"
    ["month_name"]=>string(3) "Dec"
    ["full_date"]=>string(13) "2016-December"
}

如何查找和删除这些重复的数组,以便正确计算我的独特视图,在这种情况下,此ip(111.11.11.111)将仅在12月重复一次,在11月仅重复一次。

我在下面尝试过,但它没有给我想要的结果,并且数组中缺少一些ips:

array_diff_assoc( $encodedArray, array_unique( $encodedArray ) );

有什么想法吗?

更新

我也试过使用count()函数。计数工作正常,但重复的IP计数仍然在最终计数。我的查询如下:

$wpdb->get_results("SELECT COUNT(DISTINCT IP) as all_uniques, DATE_FORMAT( insertion_date,
'%b' ) as month_name, DATE_FORMAT( insertion_date,'%Y-%M' ) as full_date FROM
post_daily_unique_views WHERE page_id = '".$unique_id_item."'");

0 个答案:

没有答案