Distinct_by

Distinct_by removes duplicated items based on the key defined and returns distinct elements from the array.

Syntax

array distinct(array $input, &key_function)

Parameters

$input: The array which to make distinct.
&key_function: Path to the value to distinct by.

Example

{
    "inputA": [
        { "name": "Bob", "id": 0 },
        { "name": "Ann", "id": 0 },
        { "name": "Joe", "id": 1 },
        { "name": "Xavier", "id": 1 }
    ]
}
{
    result: distinct_by(inputA, &id)
}
{
    "result": [
        {
            "name": "Bob",
            "id": 0
        },
        {
            "name": "Joe",
            "id": 1
        }
    ]
}

The function evaluates the duplicates with the defined key parameter in an array and returns the first distinct value. In the above example, The function removes duplicates in the array “inputA” with the key parameter “id” and returns the first distinct value of the array.

The function has two parameters. The input data (the array in which the distinct function is applied) and the Key_function ( the parameters to distinct by) and returns an array of data where the result of the property is distinct.