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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 强制数组至少有一个具有特定值的项目脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在制作一个综合的社交媒体饲料课程,可以从FaceBook获取帖子,博客和推特上的推文.然后它将它们组合成一个列表以在站点显示.问题是,很少看到任何一种帖子类型,因为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++;
}

但不是很确定我喜欢它

解决方法

你可以这样做:

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

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 强制数组至少有一个具有特定值的项目全部内容,希望文章能够帮你解决php – 强制数组至少有一个具有特定值的项目所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。