mercredi 10 mai 2017

Laravel collections: Flatten with full key name

Is there a way with Laravel Collections to flatten an array with key "namespace". Something like:

$a = collect([
    'id' => 1,
    'data' => [
        'a' => 2,
        'b' => 3
    ]
]);

$a = $a->flattenWithKeysNamespace(); // <-- this does not exists

// Should returns: 
// ['a' => 1, 'data.b' => 2, 'data.c' => 3]; // <-- I would like this.

I know I can do this in raw PHP, or with some assembly of Collection functions, but sometimes I miss something in Laravel Collection documentation. So is there a simple way with Collection functions to do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire