2011-11-01 37 views
4

我一直在制作一个组合的社交媒体提要类,它从Facebook获取帖子,并从Twitter发布博客和推文。然后将它们组合成一个列表,以便在网站上显示。问题在于,很难看到任何一种帖子类型,因为twitter比另外两种更活跃。强制一个数组拥有至少一个具有特定值的项目

我设法让它始终显示至少每种类型中的一种,但是我通过计算最终数组中每种类型的数量然后将它们拼接到最后(如果没有)并且想知道是否存在是一个更优雅的解决方案呢?

我的数组包含一堆数组,每个数组都有一个“类型”值,这是我需要测试/至少有一个每个。拼接前

Array 
(
    [0] => Array 
     (
      [id] => 131403235838803968 
      [from] => foo 
      [sent] => 1320163947 
      [type] => tweet 
      [html] => bar 
     ) 

    [1] => Array 
     (
      [id] => 131403233250914304 
      [from] => foo 
      [sent] => 1320163946 
      [type] => tweet 
      [html] => bar 
     ) 

    [2] => Array 
     (
      [id] => 131403232835674113 
      [from] => foo 
      [sent] => 1320163946 
      [type] => tweet 
      [html] => bar 
     ) 

    [3] => Array 
     (
      [id] => 131403230910480384 
      [from] => foo 
      [sent] => 1320163946 
      [type] => tweet 
      [html] => bar 
     ) 

    [4] => Array 
     (
      [id] => 131403228834299904 
      [from] => foo 
      [sent] => 1320163945 
      [type] => tweet 
      [html] => bar 
     ) 

    [5] => Array 
     (
      [type] => facebook 
      [from] => foo 
      [html] => bar 
      [sent] => 1320065996 
     ) 

    [6] => Array 
     (
      [type] => facebook 
      [from] => foo 
      [html] => bar 
      [sent] => 1319808945 
     ) 

    [7] => Array 
     (
      [type] => facebook 
      [from] => foo 
      [html] => bar 
      [sent] => 1319789640 
     ) 

    [8] => Array 
     (
      [type] => facebook 
      [from] => foo 
      [html] => bar 
      [sent] => 1319707799 
     ) 

    [9] => Array 
     (
      [type] => facebook 
      [from] => foo 
      [html] => bar 
      [sent] => 1319617295 
     ) 

    [10] => Array 
     (
      [type] => blogger 
      [from] => foo 
      [html] => bar 
      [sent] => 1320157500 
     ) 

    [11] => Array 
     (
      [type] => blogger 
      [from] => foo 
      [html] => bar 
      [sent] => 1320148260 
     ) 

) 

后它只会有5最新的。不过,我希望它有五个最新的,但确保它至少有一个与类型'博客'和一个类型'脸谱'在最后一个数组中。

得到它的工作使用约翰尼·克雷格的想法,下面的代码:

$output = array(); 
$output[] = $tweets[0]; 
$output[] = $items[0]; 
$output[] = $posts[0]; 
$feed = array_merge($tweets, $items, $posts); 
$i = 0; 
while ($limit > count($output)) { 
    if (!in_array($feed[$i], $output)) { 
     $output[] = $feed[$i]; 
    } 
    $i++; 
} 

但真的不知道我喜欢它

+0

向我们展示一些示例代码。 –

+0

@Interstellar_Coder你想要一个例子吗? – ianbarker

+0

给我们一个在拼接前和拼接后输入的样本数组,以便我们可以看到预期的结果。 –

回答

1

我最终根据@Johnny Craig的想法使用了以下内容。

if (!empty($tweets)) $output[] = $tweets[0]; 
if (!empty($items)) $output[] = $items[0]; 
if (!empty($posts)) $output[] = $posts[0]; 
$feed = array_merge($tweets, $items, $posts); 
$i = 0; 
while ($limit > count($output)) { 
    if (!in_array($feed[$i], $output)) { 
     $output[] = $feed[$i]; 
    } 
    $i++; 
} 

哪一个是与上述相同,不同之处在于我需要添加在if (!empty())比特柜面什么都没有某种类型的。

1

你可以那样做:

  • 获得第一5条目
  • 查询,不管他们是否仅仅是推特
  • 如果是的话,首先从类型=脸书或博客的阵列中拉出

问题是:什么是更快?通过一个查找类型来分割数组或迭代。

编辑: 另一种方法是计算你的twitter帖子,所以当遍历你的数组时, $ twitter = 3,然后您将匹配参数更改为!= twitter

+0

它不会总是Twitter的最新帖子,这只是最常见的 – ianbarker

+0

好吧,所以你想要的,不依赖于特殊类型,其中有5条评论之一?好的,改变了这种情况。所以你必须计算每种类型,这可能是非常错误的设计 – Stephan

0

检查每种类型的“至少一个帖子”可能会导致多次迭代,以确定哪个帖子应该交换为不同类型的帖子。考虑一下:如果最终的feed数组包含类型为{tweet,tweet,tweet,tweet,facebook}的帖子,那么将需要一个“博客”帖子,但是仅仅弹出最旧的帖子并推送最新的帖子是不够的。应用这种逻辑会给我们留下{tweet,tweet,tweet,tweet,blogger}类型的帖子。相反,应用任何给定类型的“不超过三个帖子”的限制将确保最终的馈送数组永远不会被一个帖子类型充斥。喜欢的东西:

// Get our first three newest posts, tracking their types 
for($count["blogger"] = $count["facebook"] = $count["tweet"] = $p = 0 ; $p < 3 ; $p++) 
{ 
    // Add the newest post 
    $finalFeedArray[$p] = $aggregatedFeedsArray[$p]; 

    // Increment the count for that post's type 
    $count[ $finalFeedArray[$p]["type"] ] += 1; 
} 

// Fill the remaining two slots 
for($q = 3 ; count($finalFeedArray) < 5 ; $q++) 
{ 
    // If the next newest post's type does not occur three or more times 
    if($count[ $aggregatedFeedsArray[$q]["type"] ] < 3) 
    { 
    // Add the post 
    $finalFeedArray[] = $aggregatedFeedsArray[$q]; 

    // Increment the count for that post's type 
    $count[ $aggregatedFeedsArray[$q]["type"] ] += 1; 
    } 
} 

编辑:作为zcei提到,它可能是有问题的,以在将来添加日志类型。考虑这个抽象的文章类型,明确较少变体

// Type counting array 
$count = array(); 
// Get our first three newest posts, tracking their types 
for($p = 0 ; $p < 3 ; $p++) 
{ 
    // Add the newest post 
    $finalFeedArray[$p] = $aggregatedFeedsArray[$p]; 

    // Increment the count for that post's type 
    $count[ $finalFeedArray[$p]["type"] ] += 1; 
} 

// Fill the remaining two slots 
for($q = 3 ; count($finalFeedArray) < 5 ; $q++) 
{ 
    // If the next newest post's type does not occur three or more times 
    if($count[ $aggregatedFeedsArray[$q]["type"] ] < 3) 
    { 
    // Add the post 
    $finalFeedArray[] = $aggregatedFeedsArray[$q]; 

    // Increment the count for that post's type 
    $count[ $aggregatedFeedsArray[$q]["type"] ] += 1; 
    } 
} 

需要注意的是,虽然该类型抽象是很重要的,这仍然帽我们在5帖子最多。如果您希望获得更健全的饲料建筑解决方案,请直接告诉我,但上述任何一项都应满足您当前的需求。

相关问题