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?
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));
Swadq
$count = 0;
foreach ($array as $type) {
$count+= count($type);
}
Verwenden Rekursiv zählen und subtrahieren Sie die Zählung der ersten Ebene so 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]);
Wyktor
Der einfachste Weg, dies zu tun, ist eine einfache foreach-Schleife:
$count = 0;
foreach( $tickets as $ticketType){
$count += count( $ticketType);
}
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
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;
110114 0 0 cookie-check Anzahl der mehrdimensionalen Arrays in PHP yes
@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