mardi 20 mars 2018

Laravel 5: Return the values of an array from a specified list of keys using dot notation

I am using Laravel 5.6.

I want a function that returns only the specified key / value pairs from the given array using the dot notation.

For example:

$array = [
    'name' => [
        'first' => 'John', 
        'last' => 'Smith'
    ], 
    'price' => 100,
    'orders' => 10
];


$slice = someFunc($array, ['name.first', 'price']);

should return:

[
    'name' => [
        'first' => 'John'
    ],
    'price' => 100,
]

The closest function that does this is in Laravel that I can find is the array_only function:

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

However, it does not support the dot notation (unlike some other Laravel functions).

How can I achieve this?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2DHGrUW
via IFTTT

Aucun commentaire:

Enregistrer un commentaire