Ad
Laravel Pluck Value And Append Additional String
I am getting the list of books through pluck
$outputArray = $list->pluck('title', 'id')->toArray();
so I have a list of all possible books. Then through $books_selected = $books->pluck('title', 'id')->toArray();
I have a selected books. In my output array I would like to appened True if the id is in $books_selected and false otherwise. Is there a way to do it?
Ad
Answer
One way would be to use Collections#map
:
$outputArray = $list
->pluck('title', 'id')
->map(function ($title, $id) use ($books_selected) {
return [$title, in_array($id, $books_selected)];
});
Or in a shorter form if you're using PHP 7.4:
$outputArray = $list
->pluck('title', 'id')
->map(fn($title, $id) => [$title, in_array($id, $books_selected)]);
Your final array should be in this form:
[
id1 => ['title1', true],
id2 => ['title2', false],
...
]
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad