在多维数组中计数和求和

时间:2016-02-24 08:48:03

标签: php arrays multidimensional-array count sum

Array
(
    [0] => Array
        (
            [0] => 12
            [1] => 15
            [2] => 6
        )

    [1] => Array
        (
            [0] => 6
            [1] => 9
            [2] => 11
        )

    [2] => Array
        (
            [0] => 6
            [1] => 13
            [2] => 15
            [3] => 2
            [4] => 4
            [5] => 9
        )

    [3] => Array
        (
            [0] => 12
            [1] => 15
            [2] => 1
            [3] => 6
            [4] => 7
            [5] => 8
        )

)

我想要计算每个关键值...及其总和......例如。数组1包含3个键,数组2包含3个键,数组3包含6个键,数组4包含6个键及其总和。例如.18

2 个答案:

答案 0 :(得分:2)

您可以使用以下代码。

Private Sub SaveDataButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveDataButton.Click
    '     Dim Writer As New BinaryWriter(File.Open("C:/test.bin", FileMode.Create))
    '    Dim numBytes As Long = tempArr.Length
    '   Writer.Write("R")
    '  Writer.Write(456)
    ' Writer.Write(456.789)
    ' Writer.Write("please test string here")


    '   Dim fStream As New FileStream(LocationOfFirmware.FileName + "_tmp_new", FileMode.Open, FileAccess.Write)
    '   Dim br As New BinaryWriter(fStream)

    '   tempArr = br.Write(CInt(numBytes))


    Dim newbytes() As Byte = TempArr2
    Dim newstring = UnicodeBytesToString(newbytes)


    '     CurrentArticleTextBox.Text = Mid(string2, 1652, 19)

    Dim temparr5 = NewSerialNumber.Text

    Dim SerialNumberOffset As Integer = 1625 ' Offset van het serienummer in de gemodificeerde array (1625, 11 bytes)
    Dim ArticleNumberOffset As Integer = 1643 ' Offset van het artikelnummer in de gemodificeerde array (1643, 19 bytes)


    Dim MacAddress1Offset As Integer = 1676 ' Offset van MAC Adres #1 in de gemodificeerde array (1676, 17 bytes)
    Dim MacAddress2Offset As Integer = 2014 ' Offset van MAC Adres #2 in de gemodificeerde array (2014, 17 bytes)
    Dim MacAddress3Offset As Integer = 2088 ' Offset van MAC Adres #3 in de gemodificeerde array (2088, 17 bytes)
    Dim MacAddress4Offset As Integer = 2155 ' Offset van MAC Adres #4 in de gemodificeerde array (2155, 17 bytes)


    ' Nieuw serienummer
    For i = SerialNumberOffset To SerialNumberOffset + 10
        newbytes(i) = AscW(NewSerialNumber.Text(i - SerialNumberOffset))
    Next

    '' Nieuw artiekelnummer
    For i = ArticleNumberOffset To ArticleNumberOffset + 18
        newbytes(i) = AscW(NewArticleNumber.Text(i - ArticleNumberOffset))
    Next

    ' Nieuw MAC adres #1
    For i = MacAddress1Offset To MacAddress1Offset + 16
        newbytes(i) = AscW(NewMac1TextBox.Text(i - MacAddress1Offset))
    Next
    '' Nieuw MAC adres #2
    For i = MacAddress2Offset To MacAddress2Offset + 16
        newbytes(i) = AscW(NewMac2TextBox.Text(i - MacAddress2Offset))
    Next
    ' Nieuw MAC adres #3
    For i = MacAddress3Offset To MacAddress3Offset + 16
        newbytes(i) = AscW(NewMac3TextBox.Text(i - MacAddress3Offset))
    Next
    ' Nieuw MAC adres #4
    For i = MacAddress4Offset To MacAddress4Offset + 16
        newbytes(i) = AscW(NewMac4TextBox.Text(i - MacAddress4Offset))
    Next


    NewCheckSum.Text = Format(Crc32.ComputeChecksum(newbytes), "X")



    Dim FinalBytes() As Byte

    Dim final2bytes(2559) As Byte




    '     MsgBox("7: " + NewCheckSum.Text(5))
    '    MsgBox("6: " + NewCheckSum.Text(4))
    '   MsgBox((Asc(NewCheckSum.Text(5)) + Asc(NewCheckSum.Text(4))))



    '     Dim byte0 = Convert.ToByte(NewCheckSum.Text(6))
    '    Dim byte1
    '   Dim b

    'MsgBox(
    '                    (Asc(NewCheckSum.Text(1)) + Asc(NewCheckSum.Text(0)))
    '       )
    '     (Asc(NewCheckSum.Text(6)) + Asc(NewCheckSum.Text(7))) +
    '    (Asc(NewCheckSum.Text(5)) + Asc(NewCheckSum.Text(4))) +
    '    (Asc(NewCheckSum.Text(3)) + Asc(NewCheckSum.Text(2))) +

    Dim checksumbytes = MyStringConversions.StringToByteArray(NewCheckSum.Text)
    FinalBytes = {&H80, &H8, &H0, &H0,
    checksumbytes(3), checksumbytes(2), checksumbytes(1), checksumbytes(0)
                 }


    Array.ConstrainedCopy(FinalBytes, 0, final2bytes, 0, 8)
    Array.ConstrainedCopy(newbytes, 0, final2bytes, 8, newbytes.Length)




    '        (Asc(NewCheckSum.Text(6)) + Asc(NewCheckSum.Text(7)) - 36),
    ''       (Asc(NewCheckSum.Text(5)) + Asc(NewCheckSum.Text(4)) - 36),
    '    (Asc(NewCheckSum.Text(3)) + Asc(NewCheckSum.Text(2)) - 103),
    '    (Asc(NewCheckSum.Text(1)) + Asc(NewCheckSum.Text(0)) - 103)
    '    }





    Dim fStream4 As New FileStream("" + LocationOfFirmware.FileName.Remove(FirmwareFileLocation.Length - 3) + "_final.dd", FileMode.OpenOrCreate, FileAccess.Write)

    Dim br4 As New BinaryWriter(fStream4)

    br4.Write(final2bytes)
    br4.Close()



    fStream4.Close()

    '  Dim fStream5 As New FileStream(FirmwareFileLocation.Remove(FirmwareFileLocation.Length - 3) + "_tmp_final1.dd", FileMode.OpenOrCreate, FileAccess.Write)
    '  Dim br5 As New BinaryWriter(fStream5)
    '  br5.Write(FinalBytes)
    '  br5.Close()
    '  fStream5.Close()


    'Dit hoeft pas wanneer de .dd file een goede serienummer heeft gekregen..
    'Process.Start("cmd.exe", "/C" + CopyDataCommandTextbox.Text)
    'Process.Start("cmd.exe", "/C" + WriteDataCommandTextbox.Text)

End Sub

<强>输出:

数组0有3个键

数组1有3个键

数组2有6个键

答案 1 :(得分:0)

尝试使用以下代码。它循环并获取每个数组的键数和每个数组上的值的总和。

<?php
$tester = array(0=>array(0=>12,1=>15,2=>6), 1=>array(0=>6,1=>9,2=>11), 2=>array(0=>6,1=>13,2=>15,3=>2,4=>4,5=>9));


for ($i=0; $i < sizeof($tester); $i++) {
    $sum = 0;
    for ($ii=0; $ii < sizeof($tester[$i]); $ii++) { 
        $sum += $tester[$i][$ii];
    }
    echo "Array $i has " . sizeof($tester[$i]) . " Keys. The sum of values on Array $i : $sum <br>";
}

?>

<强>输出:

数组0有3个键。数组0的值之和:33

数组1有3个键。数组1的值之和:26

数组2有6个键。数组2的值之和:49