phpnomad / utils

Library of common utilities
GNU General Public License v3.0
0 stars 0 forks source link

Support dot keys in `Arr::pluck` #4

Open malayladu opened 2 weeks ago

malayladu commented 2 weeks ago

It would be a good if Arr::pluck process the dot keys.

For example, If we want to process the slug of category from the below array, we can do using Arr::pluck($categories, 'category.slug')


[
  0 => [
    'product_id' => 7,
    'category_id' => 1,
    'id' => 1,
    'created' => '2024-11-08 10:34:29',
    'category' => [
      'name' => 'Movies',
      'slug' => 'movies',
      'id' => 1,
      'created' => '2024-11-08 10:34:29',
    ],
  ],
  1 => [
    'product_id' => 7,
    'category_id' => 2,
    'id' => 2,
    'created' => '2024-11-08 10:34:29',
    'category' => [
      'name' => 'Sports',
      'slug' => 'sports',
      'id' => 2,
      'created' => '2024-11-08 10:34:29',
    ],
  ],
  2 => [
    'product_id' => 7,
    'category_id' => 3,
    'id' => 3,
    'created' => '2024-11-08 10:34:29',
    'category' => [
      'name' => 'Politics',
      'slug' => 'politics',
      'id' => 3,
      'created' => '2024-11-08 10:34:29',
    ],
  ],
]
alexstandiford commented 2 weeks ago

Is the goal in this circumstance to return an array of category slugs from each one, in this case?

malayladu commented 2 weeks ago

Yes, it should return slugs from each one.

$categories = [
 'movies',
 'sports',
 'politics',
];