Skip to content

2 new methods in \app\framework\Component\StdLib\StdObject\ArrayObject\ManipulationTrait #9

@MrFibunacci

Description

@MrFibunacci

First:

map()
The map method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:

$collection = collect([1, 2, 3, 4, 5]);

$multiplied = $collection->map(function ($item, $key) {
    return $item * 2;
});

$multiplied->all();

// [2, 4, 6, 8, 10]

Warning
Unlike most other ArrayObject methods, map returns a new collection instance; it does not modify
the ArrayObject it is called on. If you want to transform the original ArrayObject, use the transform
method.

Second

implode()
The implode method joins the items in a ArrayObject. Its arguments depend on the type of items in the collection. If the collection contains arrays or objects, you should pass the key of the attributes you wish to join, and the "glue" string you wish to place between the values:

$arr = arr([
    ['account_id' => 1, 'product' => 'Desk'],
    ['account_id' => 2, 'product' => 'Chair'],
]);

$arr->implode('product', ', ');

// Desk, Chair

If the ArrayObject contains simple strings or numeric values, pass the "glue" as the only argument to the method:

arr([1, 2, 3, 4, 5])->implode('-');

// '1-2-3-4-5'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions