Skip to content

array counting

Alex Oliver edited this page May 21, 2026 · 1 revision

Array Counting

Brace can count the number of items in an array using the COUNT() function inside {{ }} expressions.


Displaying a count

<?php

use Brace\Parser;

$brace = new Parser();

echo $brace->parseInputString(
    '<p>Total items: {{ COUNT(products) }}</p>',
    [
        'products' => [
            ['name' => 'Widget'],
            ['name' => 'Gadget'],
            ['name' => 'Doohickey'],
        ],
    ],
    false
)->return();
// Output: <p>Total items: 3</p>

Using a count in a conditional

COUNT() can be used anywhere a value is expected in an {{ if }} condition.

{{ if COUNT(cart) === 3 }}
<p>There are exactly three items.</p>
{{ end }}

Count with comparison operators

All standard condition operators work with COUNT().

{{ if COUNT(cart) > 0 }}
<p>You have {{ COUNT(cart) }} item(s) in your cart.</p>
{{ else }}
<p>Your cart is empty.</p>
{{ end }}
{{ if COUNT(results) >= 10 }}
<p>Showing first 10 of {{ COUNT(results) }} results.</p>
{{ end }}

Nested array count

COUNT() works with nested paths using the -> operator.

<?php

use Brace\Parser;

$brace = new Parser();

echo $brace->parseInputString(
    '{{ COUNT(order->items) }} item(s) ordered.',
    [
        'order' => [
            'items' => ['Book', 'Pen', 'Ruler'],
        ],
    ],
    false
)->return();
// Output: 3 item(s) ordered.

Clone this wiki locally