update page now
Laravel Live Japan

SimpleXMLElement::count

(PHP 8)

SimpleXMLElement::countZählt die Anzahl der Kinder eines Elements

Beschreibung

public SimpleXMLElement::count(): int

Diese Methode zählt die Anzahl der Kinder eines Elements.

Parameter-Liste

Diese Funktion besitzt keine Parameter.

Rückgabewerte

Gibt die Anzahl der Elemente eines Elements zurück.

Beispiele

Beispiel #1 Zählen der Kinder

<?php
$xml
= <<<EOF
<people>
<person name="Person 1">
<child/>
<child/>
<child/>
</person>
<person name="Person 2">
<child/>
<child/>
<child/>
<child/>
<child/>
</person>
</people>
EOF;

$elem = new SimpleXMLElement($xml);

foreach (
$elem as $person) {
printf("%s hat %d Kinder.\n", $person['name'], $person->count());
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Person 1 hat 3 Kinder.
Person 2 hat 5 Kinder.

Siehe auch

add a note

User Contributed Notes 1 note

up
-3
daniel dot erni at focusedpublishing dot com
14 years ago
Alternative code for PHP < 5.3:
<?php
$count = $node->count(); / PHP > 5.3
$count = count($node->children()); / PHP < 5.3
?>
To Top

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant