Skip to content

Array display drops string keys #13

Description

@MarcelloDuarte

Associative arrays render as if they were lists, so the keys are simply not shown:

phunkie > ["a" => 1, "b" => 2]
$var0: Array = [1, 2]           <-- expected ["a" => 1, "b" => 2]

phunkie > ["x" => ["y" => 1]]
$var1: Array = [[1]]            <-- nested too

EvaluationResult::formatArray() (src/Types/EvaluationResult.php:83) maps over values and discards keys:

private function formatArray(array $arr): string
{
    $items = array_map(fn($v) => $this->formatValue($v), $arr);
    return '[' . implode(', ', $items) . ']';
}

This is misleading rather than merely terse: two different arrays print identically, and there is no way to tell from the output whether a key was a string or an integer. It matters most for the array functions people reach for in a REPL - array_first/array_last/array_find_key on an associative array all print results whose provenance is invisible.

Suggested: print key => value when the array is not a list (array_is_list()), and keep the current compact form when it is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions