如何在php中计算完整的项目集


How to count complete sets of items in php?

我是一个新手,所以希望我能正确地使用这个词。我在SO和网络上搜索了又搜索,试图找到这个答案,甚至在数学上,了解如何编码,但都无济于事。

在网络游戏中,我希望能够计算用户拥有的完整游戏集的数量(该游戏集有四个项目)。

例如,用户可以获得火鸡、南瓜派、馅料和土豆泥。这些按数量显示在表格中,如下所示:

土豆泥:2
土耳其:4
南瓜派:4
填充:3

因此,在这种情况下,用户总共有2套我称之为"感恩节晚餐"的东西。感恩节晚餐必须包括每一项中的一项才能成为一套。

我如何使用PHP来计算和显示用户拥有的完整集的数量?

这感觉应该很简单(至少在数学上),但我真的很挣扎。

简单地尝试一下,如PHP Fiddle中所示

<?php
    $min = 100000; // just initialize min with large value.
    // these values can be hard coded, from JSON, html form, or database
    // it is just written for illustration
    $arr = array(
        'Mashed Potatoes' => 2,
        'Turkey' => 4,
        'Pumpkin Pie'=> 4,
        'Stuffing'=> 3
    );
    // we loop through each element of the above array and,
    // compare its value to the min.
    foreach($arr as $value){
        if( $value < $min){
            // If the value is less that min. then we store value into min
            $min = $value;
        }
    }
    echo 'You can have ' . $min . ' Thanksgiving dinners';
?>

编辑:正如PaparazzoKi在评论中所建议的那样,谢谢,你可以替换所有这些:

foreach($arr as $value){
        if( $value < $min){
            // If the value is less that min. then we store value into min
            $min = $value;
        }
    }
    echo 'You can have ' . $min . ' Thanksgiving dinners';

使用此行:

echo 'You can have ' . min($arr) . ' Thanksgiving dinners';

利用.min()函数

完整集合的总数将是数组中值最低的键。

您可以使用PHP 的min()函数

http://php.net/manual/en/function.min.php