Anzahl der mehrdimensionalen Arrays in PHP

Lesezeit: 3 Minuten

Ich habe ein mehrdimensionales Array wie folgt eingerichtet

array() {
    ["type1"] =>
    array() {
        ["ticket1"] =>
        array(9) { 
           // ticket details here
        }
        ["ticket2"] =>
        array(10) { 
           // ticket details here
        }
        ["ticket3"] =>
        array(9) { 
           // ticket details here
        }
    }
    ["type2"] =>
    array() {
        ["ticket1"] =>
        array(9) { 
           // ticket details here
        }
        ["ticket2"] =>
        array(10) { 
           // ticket details here
        }
        ["ticket3"] =>
        array(9) { 
           // ticket details here
        }
    }
}

usw.

Ich versuche, die Gesamtzahl der Tickets im Array zu zählen (Anzahl der Elemente der zweiten Ebene), aber die Anzahl der Typen ist variabel, ebenso wie die Anzahl der Felder, die jedes Ticket hat, sodass ich COUNT_RECURSIVE und Mathematik nicht verwenden kann Nummer bekommen.

Kann mir jemand helfen?

  • @MadaraUchiha Eine Zähladdition innerhalb einer Schleife (je nachdem, was am besten ist) ist möglicherweise besser, da Sie nicht alles iterieren müssen.

    – Mein1

    27. April 2018 um 8:14 Uhr

Benutzer-Avatar
großekm

Ein bisschen spät, aber das ist eine saubere Art, es zu schreiben.

$totalTickets = array_sum(array_map("count", $tickets));

Vorausgesetzt $tickets ist Ihr mehrdimensionales Array.

Ich habe den Code erweitert, um ein erklärtes Beispiel zu geben, weil array_map vielleicht für manche neu

$tickets = array(
    "type1" => array(
        "ticket1" => array(),
        "ticket2" => array(),
        "ticket3" => array(),
    ),
    "type2" => array(
        "ticket4" => array(),
        "ticket5" => array(),
        "ticket6" => array(),
        "ticket7" => array(),
    ),
    "type3" => array(
        "ticket8" => array()
    )
);

// First we map count over every first level item in the array
// giving us the total number of tickets for each type.
$typeTotals = array_map("count", $tickets);

// print_r($typeTotals);
// $type_totals --> Array (
//                       [type1] => 3,
//                       [type2] => 4,
//                       [type3] => 1 
//                  )

//Then we sum the values of each of these
$totalTickets = array_sum($typeTotals);

print($totalTickets);
// $totalTickets --> 8

Da wir uns also nicht um das Zwischenergebnis jedes Typs kümmern, können wir das Ergebnis einspeisen array_summe

$totalTickets = array_sum(array_map("count", $tickets));

Benutzer-Avatar
Swadq

$count = 0;
foreach ($array as $type) {
    $count+= count($type);
}

Verwenden Rekursiv zählen und subtrahieren Sie die Zählung der ersten Ebeneso was:

count($mainArr, COUNT_RECURSIVE) - count($mainArr);

Wenn Ihr Array +3 Ebenen hat, fügen Sie einfach hinzu [?] Schlüssel:

count($mainArr[1], COUNT_RECURSIVE) - count($mainArr[1]);

  • Was ist, wenn ich ein 4-dimensionales Array habe?

    – nashwa

    15. April 2017 um 18:12 Uhr

  • Exzellent! @nashwa, rekursiv zählt jedes Element IIRC, egal wie tief

    – Mazzig

    3. August 2019 um 2:50 Uhr

  • Ich wünschte, ich könnte den Ruf geben, mehr als einmal zu stimmen. Warum ist dies nicht die akzeptierte Antwort?

    – WEBjuju

    21. November 2019 um 17:27 Uhr

  • Für mich count($arr, COUNT_RECURSIVE) war eine ausreichende, aber großartige Antwort, da sie mehr Tiefe ermöglicht

    – Antonius

    3. Februar 2021 um 13:49 Uhr

Benutzer-Avatar
Wyktor

Der einfachste Weg, dies zu tun, ist eine einfache foreach-Schleife:

$count = 0;
foreach( $tickets as $ticketType){
    $count += count( $ticketType);
}

Benutzer-Avatar
xhdix

🙂

$test = array (
    array (
        'test','test','test'
    ),
    array (
    'test','test'
    ),
    array (
        array (
        'test'
        ),
        array (
        'test','test','test','test'
        )
    )
 );
 echo "  array count ". count($test[0]) ." <br /> ";
 echo "  array count ". count($test[1]) ." <br /> ";
 echo "  array count ". count($test[2]) ." <br /> ";
 echo "  array count ". count($test[2],1) ." <br /> ";
 echo "  array count ". (count($test[2],1) - count($test[2])) ." <br /> "; // all child num - parent num
 echo "  array count ". count($test[2][1]) ." <br /> ";

Ausgang:

Array-Anzahl 3

Array-Anzahl 2

Array-Anzahl 2

Array-Anzahl 7

Array-Anzahl 5

Array-Anzahl 4

Benutzer-Avatar
Saiful Islam

Um die Gesamtzahl der Tickets zu zählen, hilft Ihnen dieser untenstehende Code für PHP.

foreach($mainArray as $Array){
    foreach($Array as $perTicke){
        $count++;
    }
}
$total_ticket = $count;

1101140cookie-checkAnzahl der mehrdimensionalen Arrays in PHP

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy