mercredi 5 septembre 2018

Laravel array_only with dot notation

Laravel has a helper that allows you to get only the keys you want like so:

https://laravel.com/docs/5.6/helpers#method-array-only

$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];

$slice = array_only($array, ['name', 'price']);

// ['name' => 'Desk', 'price' => 100]

However, how can I get the array_only equivalent that also allows dot notation. e.g.

$array = [
    'name' => [
        'first' => 'John',
        'last' => 'Smith',
    ],
    'levels' => [
        'maths' => 6,
        'science' => 10,
    ],
    'age' => 25,
];

$slice = array_dot_only($array, ['name.first', 'levels']);

/*
[
    'name' => [
        'first' => 'John',
    ],
    'levels' => [
        'maths' => 6,
        'science' => 10,
    ],
];
*/

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire