强制数组至少有一个具有特定值的项目

时间:2011-11-01 16:06:55

标签: php

我一直在制作一个综合的社交媒体供稿课程,可以从Facebook上获取帖子,也可以从Twitter上发布博客和推文。然后它将它们组合成一个列表以在站点上显示。问题是,很少看到任何一种帖子类型,因为Twitter比其他两种更活跃。

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

我的数组包含一堆数组,每个数组都有一个'type'值,这是我需要测试的/至少有一个。

在拼接之前

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个。但是我希望它有最新的五个,但要确保它至少有一个类型'blogger'和一个类型为'facebook'的最终数组。

使用Johnny Craig的想法和以下代码了解它:

$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++;
}

但不确定我喜欢它

3 个答案:

答案 0 :(得分:1)

你可以这样做:

  • 获取前5个条目
  • 检查,他们是否只是推特
  • 如果是,请先从数组中输入type = facebook或blogger

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

编辑: 另一种方法是计算你的推文帖子,所以在迭代你所拥有的数组时,例如$ twitter = 3,然后将匹配参数更改为!= twitter

答案 1 :(得分: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())位之外,其中没有某种类型,这与上面相同。

答案 2 :(得分: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个帖子。如果您需要更强大的饲料构建解决方案,请直接告诉我,但上述任何一种都应满足您当前的需求。