Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_unique()

Return the first entry of each group of equal values and preserve its key.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

array_unique(array $array, int $flags = SORT_STRING): array

Return the first entry of each group of equal values and preserve its key. SORT_STRING, the default, compares string representations. SORT_NUMERIC compares numerically, SORT_REGULAR uses ordinary PHP comparison, and SORT_LOCALE_STRING compares text according to the current locale. Removing duplicates can leave index gaps. Reindex the result when consecutive positions are required.

Signature

array_unique(array $array, int $flags = SORT_STRING): array

Example output

[{"0":7,"2":"07","3":9},{"0":7,"3":9}]

Examples

<?php
$items = [7, '7', '07', 9];
echo json_encode([array_unique($items, SORT_STRING), array_unique($items, SORT_NUMERIC)]);

Result

[{"0":7,"2":"07","3":9},{"0":7,"3":9}]

Sources