Preserve a JSON list

Take your time, follow the code and check your understanding.

The codePHP arrays and requests
<?php
$rows = [0 => "A", 1 => "B", 2 => "C"];
$filtered = array_filter($rows, fn($v) => $v !== "B");
echo json_encode($filtered);
Your answer4 min · no timer
What needs fixing?

Show solution and explanation

array_filter preserves keys. Non-contiguous numeric keys encode as a JSON object; array_values reindexes them into a list.

Use array_values($filtered) before JSON encoding if a JSON array is required.