Php 获取Foreach循环中元素的最高值

Php 获取Foreach循环中元素的最高值,php,arrays,foreach,Php,Arrays,Foreach,我试图找出foreach循环何时结束,这样我就可以关闭HTML标记,并将这个问题修改得更简洁一些。我现在将数据集作为数组传递到函数中,希望它能使它变得更简单。我需要确定其中一个字段何时达到最大值,这是我在每次通过foreach之后无法计算出来的,它“认为”它在下一次通过之前处于最大值,以此类推 数据集数组如下所示: Array ( [0] => 3391 [ID] => 3391 [1] => 2 [SubID] => 2 [2]

我试图找出foreach循环何时结束,这样我就可以关闭HTML标记,并将这个问题修改得更简洁一些。我现在将数据集作为数组传递到函数中,希望它能使它变得更简单。我需要确定其中一个字段何时达到最大值,这是我在每次通过foreach之后无法计算出来的,它“认为”它在下一次通过之前处于最大值,以此类推

数据集数组如下所示:

Array
(
    [0] => 3391
    [ID] => 3391
    [1] => 2
    [SubID] => 2
    [2] => 2
    [ListType] => 2
    [3] => 0
    [IndentText] => 0
    [4] => 3
    [BaseGroup] => 3
    [4] => 1
    [Quantity] => 1
) 
如何在循环时获取最高的SubID值?我一直在考虑这样一个函数,但是当它与每一行一起运行时,我不知道如何获得最大值

function getMax($index, $array) {
    $array = array($array[$index]);
    return max($array);
}
我希望以$maxID=getMax(“SubID”,$array)的形式运行它,但还没有弄清楚如何实际使它产生任何有意义的结果,但一旦它起作用,那么:

if ($maxID == $row['SubID']) :
   // CLOSING HTML HERE
endif;
。。。将告诉代码它位于最后一行,标记可以关闭。有什么想法吗

这是整个函数,但实际上问题与它没有直接关系,所以我不确定它是否有用

function Notes($rowEntry) {
    // Create variables
    $ListType = (isset($rowEntry['ListType'])) ? $rowEntry['ListType'] : "";
    /* $ID = (isset($rowEntry['ID'])) ? $rowEntry['ID'] : ""; */
    $SubID = (isset($rowEntry['SubID'])) ? $rowEntry['SubID'] : "";
    $GroupNo = (isset($rowEntry['GroupNo'])) ? $rowEntry['GroupNo'] : "";
    $Title = (isset($rowEntry['Title'])) ? $rowEntry['Title'] : "";
    $SubPartNo = (isset($rowEntry['SubPartNo'])) ? $rowEntry['SubPartNo'] : "";
    $SubDescription = (isset($rowEntry['SubDescription'])) ? LinkAbbvr($rowEntry['SubDescription']) : " ";
    $SubPage = (isset($rowEntry['SubPage'])) ? $rowEntry['SubPage'] : "";
    // SubQuantity field is integer so, to match original hardcopy, 0 used in place of X
    // Condition below changes it back to X for display
    $SubQuantity = (isset($rowEntry['SubQuantity'])) ? $rowEntry['SubQuantity'] : "";
    $SubQuantity = ($SubQuantity == 0) ? "X" : $SubQuantity;
    // Create Edit link to SubQuantity value
    $EditSubQuantity = (isset($_SESSION['AccessLevel']) && $_SESSION['AccessLevel'] == 3) 
                            ? "<a href=\"/administration/adminforms.php?FormID=33&ID=$SubID\">$SubQuantity</a>"
                            : $SubQuantity;

    // For note of a specific group by itself, ie Group 3.5890 page 98
    // Uses style .groupcaption
    if ($ListType == 1) :
        if ($GroupNo && $Title) :
            $last_groupnote = "";
            if ($Title && $GroupNo != $last_groupnote) :
                $last_groupnote = $GroupNo;

                $NotesText = "<div class=\"groupcaption\">\n";
                    $NotesText .= "<div class=\"SubTitle\">$Title</div>\n";
                $NotesText .= "</div>\n\n";

            echo $NotesText;
            endif;
        endif;

    // For indented sub-listings, ie Group 3.639 page 108
    // This lists Quantity, PartNo and Description
    // Uses style #css-subtable
    elseif ($ListType == 2) :
        global $TitleCaption;
        $NotesText = "";
        // Show Title column only once
        if ($Title != $TitleCaption) :
            $TitleCaption = $Title;
            // Close part listing row table
            $NotesText .= "</div>\n\n";
            // Open subtable and title
            $NotesText .= "<div id=\"css-subtable\">\n";
            $NotesText .= "<div class=\"RowCaption\">$Title</div>\n\n";
        endif;

        if ($SubDescription && !is_null($SubDescription)) :
            $NotesText .= "<div class=\"row\">\n";
                $NotesText .= "<div class=\"col\">$EditSubQuantity</div>\n";
                $NotesText .= "<div class=\"col\">$SubPartNo</div>\n";
                $NotesText .= "<div class=\"col\">$SubDescription</div>\n";
            $NotesText .= "</div>\n\n";
        endif;

        $subMax = getMax("SubID", $rowEntry);

        // Check to see if last row
        if ($SubID == $subMax) :
        //if ($SubID == 15) :
            // Close the subtable
            $NotesText .= "</div>\n\n";
            // Restart row table
            if ($ListType < 5) :
                $NotesText .= "<div id=\"css-table\">\n";
            elseif ($ListType > 4) :
                $NotesText .= "<div id=\"css-sublist$ListType\">\n";
            endif;
        endif;

        echo $NotesText;

    // For indented sub-listings description-only list, ie Group 1.322 page 16
    // This lists Description only
    // Uses style #css-subsingletable
    elseif ($ListType == 3) :
        global $rowCount;
        $NotesText = "";

        $rowCount++;
        // Clost main style, open new one
        if ($rowCount == 1) :
            // Close part listing row table
            $NotesText .= "</div>\n\n";
            // Open subtable and title
            $NotesText .= "<div id=\"css-subsingletable\">\n";
        endif;

        if ($SubDescription && !is_null($SubDescription)) :
            $rowCount++;
            $NotesText .= "<div class=\"row\">\n";
                $NotesText .= "<div class=\"col\">$SubDescription</div>\n";
            $NotesText .= "</div>\n\n";
        endif;

        if ($rowCount == 3) :
            $NotesText .= "</div>\n\n";
            // Restart row table
            if ($ListType < 5) :
                $NotesText .= "<div id=\"css-table\">\n";
            elseif ($ListType > 4) :
                $NotesText .= "<div id=\"css-sublist$ListType\">\n";
            endif;
        endif;
        echo $NotesText;

    // For single notes, ie Group 1.607 page 29
    // Uses style #css-partnotes 
    elseif ($ListType == 4) :
        $TitleCaption = "";

        if ($Title) :
            if ($Title != $TitleCaption) :
                $TitleCaption = $Title;
                // Close part listing row table
                $NotesText = "</div>\n\n";
                    $NotesText .= "<div id=\"css-partnotes\">\n";
                        $NotesText .= "<div class=\"row\">\n";
                            $NotesText .= "<div class=\"col\">\n";
                                $NotesText .= "<div class=\"RowCaption\">$Title</div>\n";
                            $NotesText .= "</div>\n";
                        $NotesText .= "</div>\n";
                    $NotesText .= "</div>\n\n";
                // Restart row table
                if ($ListType < 5) :
                    $NotesText .= "<div id=\"css-table\">\n";
                elseif ($ListType > 4) :
                    $NotesText .= "<div id=\"css-sublist$ListType\">\n";
                endif;
                echo $NotesText;
            endif;
        endif;
    endif;
}
功能注释($rowEntry){
//创建变量
$ListType=(isset($rowEntry['ListType'])?$rowEntry['ListType']:“”;
/*$ID=(isset($rowEntry['ID'])?$rowEntry['ID']:“”*/
$SubID=(isset($rowEntry['SubID'])?$rowEntry['SubID']:“”;
$GroupNo=(isset($rowEntry['GroupNo'])?$rowEntry['GroupNo']:“”;
$Title=(isset($rowEntry['Title'])?$rowEntry['Title']:“”;
$SubPartNo=(isset($rowEntry['SubPartNo'])?$rowEntry['SubPartNo']:“”;
$SubDescription=(isset($rowEntry['SubDescription'])?LinkAbbvr($rowEntry['SubDescription']):“”;
$SubPage=(isset($rowEntry['SubPage'])?$rowEntry['SubPage']:“”;
//“子数量”字段为整数,因此为了匹配原始硬拷贝,使用0代替X
//下面的条件将其更改回X以便显示
$SubQuantity=(isset($rowEntry['SubQuantity'])?$rowEntry['SubQuantity']:“”;
$SubQuantity=($SubQuantity==0)?“X”:$SubQuantity;
//创建到子数量值的编辑链接
$EditSubQuantity=(isset($\u会话['AccessLevel'])和&$\u会话['AccessLevel']==3)
? ""
$SubQuantity;
//对于特定组本身的注释,参见第98页第3.5890组
//使用style.groupcaption
如果($ListType==1):
如果($GroupNo&&$Title):
$last_groupnote=“”;
如果($Title&&$GroupNo!=$last\u groupnote):
$last_groupnote=$GroupNo;
$NotesText=“\n”;
$NotesText.=“$Title\n”;
$NotesText.=“\n\n”;
echo$NotesText;
endif;
endif;
//对于缩进子列表,请参见第108页第3.639组
//此列表列出了数量、零件号和说明
//使用样式#css子表
elseif($ListType==2):
全球$TitleCaption;
$NotesText=“”;
//仅显示标题列一次
如果($Title!=$TitleCaption):
$TitleCaption=$Title;
//关闭部件列表行表
$NotesText.=“\n\n”;
//打开子表和标题
$NotesText.=“\n”;
$NotesText.=“$Title\n\n”;
endif;
如果($SubDescription&!为空($SubDescription)):
$NotesText.=“\n”;
$NotesText.=“$EditSubQuantity\n”;
$NotesText.=“$SubPartNo\n”;
$NotesText.=“$SubDescription\n”;
$NotesText.=“\n\n”;
endif;
$subMax=getMax(“SubID”,$rowEntry);
//检查最后一行
如果($SubID==$subMax):
//如果($SubID==15):
//关闭子表
$NotesText.=“\n\n”;
//重新启动行表
如果($ListType<5):
$NotesText.=“\n”;
elseif($ListType>4):
$NotesText.=“\n”;
endif;
endif;
echo$NotesText;
//对于缩进子列表,仅说明列表,即第16页第1.322组
//此列表仅列出说明
//使用样式#css子表格
elseif($ListType==3):
全球$行数;
$NotesText=“”;
$rowCount++;
//关闭主样式,打开新样式
如果($rowCount==1):
//关闭部件列表行表
$NotesText.=“\n\n”;
//打开子表和标题
$NotesText.=“\n”;
endif;
如果($SubDescription&!为空($SubDescription)):
$rowCount++;
$NotesText.=“\n”;
$NotesText.=“$SubDescription\n”;
$NotesText.=“\n\n”;
endif;
如果($rowCount==3):
$NotesText.=“\n\n”;
//重新启动行表
如果($ListType<5):
$NotesText.=“\n”;
elseif($ListType>4):
$NotesText.=“\n”;
endif;
endif;
echo$NotesText;
//对于单个注释,请参见第29页第1.607组
//使用样式#css partnotes
elseif($ListType==4):
$TitleCaption=“”;
如果($标题):
如果($Title!=$TitleCaption):
$TitleCaption=$Title;
//关闭部件列表行表
$NotesText=“\n\n”;
$NotesText.=“\n”;
$NotesText.=“\n”;
$NotesText.=“\n”;
$NotesText.=“$Title\n”;
$NotesText.=“\n”;
$NotesText.=“\n”;
$NotesText.=“\n\n”;
//重新启动行表
如果($ListType<5):
$NotesText.=“\n”;
elseif($ListType>4):
$NotesText.=“\n”;
endif;
回声$N
// This function used to simplify partsbook.php and partsbookdetails.php
// IN CONJUNCTION WITH ABOVE, FUNCTION PARTIALLY REPLACES OBSOLETE PartID()
function PartsList($sqlView) {
    $rowView = DBConnect($sqlView, "Multiple", "pchome_packardsimperials");
    if ($rowView) :
        $last_groupno = "";
        $last_id = "";
        $rowNumber = 0;
        foreach ($rowView as $row) :
            $rowNumber++;
            $ID = (isset($row['ID'])) ? $row['ID'] : "";
            $ListType = (isset($row['ListType'])) ? $row['ListType'] : "";
            $BaseGroup = (isset($row['BaseGroup'])) ? $row['BaseGroup'] : 0;
            $GroupNo = (isset($row['GroupNo'])) ? $row['GroupNo'] : "";
            $GroupName = (isset($row['GroupName'])) ? LinkAbbvr($row['GroupName']) : "";
            $Name = (isset($row['Name'])) ? LinkAbbvr($row['Name']) : "";
            $Title = (isset($row['Title'])) ? $row['Title'] : "";
            $PartNo = (isset($row['PartNo'])) ? $row['PartNo'] : "";

            $Models = (isset($row['Models'])) ? $row['Models'] : "";
            $Description = (isset($row['Description'])) ? LinkAbbvr($row['Description']) : "&nbsp;";
            $PageNo = (isset($row['PageNo'])) ? $row['PageNo'] : "";
            $SubPage = (isset($row['SubPage'])) ? $row['SubPage'] : "";
            $RevDate = (isset($row['RevDate'])) ? $row['RevDate'] : "";
            $Edition = (isset($row['Edition'])) ? $row['Edition'] : "";

            // Choose variable for use in title of GroupNo() function
            if ($ListType < 5) :
                $ListName = $Name;
            elseif ($ListType > 4) :
                $ListName = $Title;
            endif;

            // Check to see if GroupNo is different than previous one
            if ($GroupNo != $last_groupno || $rowNumber == 1) :
                $last_groupno = $GroupNo;
                $NewGroup = TRUE;
            else :
                $NewGroup = FALSE;
            endif;

            // Show GroupNo() captions only once per group
            if ($NewGroup === TRUE) :
                GroupNo($GroupNo, $NewGroup, $ListName, $rowNumber, $ListType);
            endif;

            if ($ListType < 5) :
                // To prevent duplicates, check to see if ID is different than previous one before displaying
                if ($ID != $last_id) :
                    $last_id = $ID;
                    PartRow($row);
                endif;
                Notes($row);
            elseif ($ListType > 4) :
                SpecialRow($row);
            endif;

        endforeach;
    endif;
}
SELECT DISTINCT 
pl.ID, 
pn.ID AS SubID, 

IF(ListType,(SELECT MAX(ID) 
FROM parts_notes 
WHERE PageNo=108 
AND ListType IS NOT NULL 
AND SubPage IS NULL ),NULL) AS SubMax,

ListType, 
IndentText, . . .