Php arrayToCollection
I have a problem with the code below. I do not understand how it works. I come from java background and this function seems weird to me. In the first parameter it returns the result and in the second parameter it returns a list.
private static function getEventsOfTheCampaigns($campaigns)
{
$events = Utility::arrayToCollection(array_map(function ($id) {
return Events::find($id);
}, array_unique(Utility::getIndexFromCollection($campaigns, 'event_id'))));
return $events;
}
defintion of the functions is below
public static function arrayToCollection(array $list){
$result = collect();
if( !is_null($list) ):
if(is_array( $list ) and count($list) > 0):
foreach($list as $item):
$result->push($item);
endforeach;
endif;
endif;
return $result;
}
can somebody explain me how it works? thanks.
php arrays laravel web collections
add a comment |
I have a problem with the code below. I do not understand how it works. I come from java background and this function seems weird to me. In the first parameter it returns the result and in the second parameter it returns a list.
private static function getEventsOfTheCampaigns($campaigns)
{
$events = Utility::arrayToCollection(array_map(function ($id) {
return Events::find($id);
}, array_unique(Utility::getIndexFromCollection($campaigns, 'event_id'))));
return $events;
}
defintion of the functions is below
public static function arrayToCollection(array $list){
$result = collect();
if( !is_null($list) ):
if(is_array( $list ) and count($list) > 0):
foreach($list as $item):
$result->push($item);
endforeach;
endif;
endif;
return $result;
}
can somebody explain me how it works? thanks.
php arrays laravel web collections
1
In the first parameter it returns the result and in the second parameter it returns a list.
- can you be more precisely? Where is second parameter? I see only two functions - each with exactly one parameter
– Kamil Kiełczewski
Nov 14 '18 at 21:01
Which function are you asking about? The second function just turns an array into a Laravel collection. It doesn't seem very useful though since collect() already does this.
– Devon
Nov 14 '18 at 21:01
The checks for!is_null($list)
,is_array($list)
, andcount($list) > 0
are all unnecessary in the second function. The parameter definition doesn't allow nulls or non-arrays to be passed, and you can foreach over an empty array with no errors.
– Alex Howansky
Nov 14 '18 at 21:02
Not to mention that an array can simply be passed tocollect()
to create the collection, so the whole function is pretty useless.
– miken32
Nov 14 '18 at 21:26
add a comment |
I have a problem with the code below. I do not understand how it works. I come from java background and this function seems weird to me. In the first parameter it returns the result and in the second parameter it returns a list.
private static function getEventsOfTheCampaigns($campaigns)
{
$events = Utility::arrayToCollection(array_map(function ($id) {
return Events::find($id);
}, array_unique(Utility::getIndexFromCollection($campaigns, 'event_id'))));
return $events;
}
defintion of the functions is below
public static function arrayToCollection(array $list){
$result = collect();
if( !is_null($list) ):
if(is_array( $list ) and count($list) > 0):
foreach($list as $item):
$result->push($item);
endforeach;
endif;
endif;
return $result;
}
can somebody explain me how it works? thanks.
php arrays laravel web collections
I have a problem with the code below. I do not understand how it works. I come from java background and this function seems weird to me. In the first parameter it returns the result and in the second parameter it returns a list.
private static function getEventsOfTheCampaigns($campaigns)
{
$events = Utility::arrayToCollection(array_map(function ($id) {
return Events::find($id);
}, array_unique(Utility::getIndexFromCollection($campaigns, 'event_id'))));
return $events;
}
defintion of the functions is below
public static function arrayToCollection(array $list){
$result = collect();
if( !is_null($list) ):
if(is_array( $list ) and count($list) > 0):
foreach($list as $item):
$result->push($item);
endforeach;
endif;
endif;
return $result;
}
can somebody explain me how it works? thanks.
php arrays laravel web collections
php arrays laravel web collections
asked Nov 14 '18 at 20:57
MohsenFMMohsenFM
428
428
1
In the first parameter it returns the result and in the second parameter it returns a list.
- can you be more precisely? Where is second parameter? I see only two functions - each with exactly one parameter
– Kamil Kiełczewski
Nov 14 '18 at 21:01
Which function are you asking about? The second function just turns an array into a Laravel collection. It doesn't seem very useful though since collect() already does this.
– Devon
Nov 14 '18 at 21:01
The checks for!is_null($list)
,is_array($list)
, andcount($list) > 0
are all unnecessary in the second function. The parameter definition doesn't allow nulls or non-arrays to be passed, and you can foreach over an empty array with no errors.
– Alex Howansky
Nov 14 '18 at 21:02
Not to mention that an array can simply be passed tocollect()
to create the collection, so the whole function is pretty useless.
– miken32
Nov 14 '18 at 21:26
add a comment |
1
In the first parameter it returns the result and in the second parameter it returns a list.
- can you be more precisely? Where is second parameter? I see only two functions - each with exactly one parameter
– Kamil Kiełczewski
Nov 14 '18 at 21:01
Which function are you asking about? The second function just turns an array into a Laravel collection. It doesn't seem very useful though since collect() already does this.
– Devon
Nov 14 '18 at 21:01
The checks for!is_null($list)
,is_array($list)
, andcount($list) > 0
are all unnecessary in the second function. The parameter definition doesn't allow nulls or non-arrays to be passed, and you can foreach over an empty array with no errors.
– Alex Howansky
Nov 14 '18 at 21:02
Not to mention that an array can simply be passed tocollect()
to create the collection, so the whole function is pretty useless.
– miken32
Nov 14 '18 at 21:26
1
1
In the first parameter it returns the result and in the second parameter it returns a list.
- can you be more precisely? Where is second parameter? I see only two functions - each with exactly one parameter– Kamil Kiełczewski
Nov 14 '18 at 21:01
In the first parameter it returns the result and in the second parameter it returns a list.
- can you be more precisely? Where is second parameter? I see only two functions - each with exactly one parameter– Kamil Kiełczewski
Nov 14 '18 at 21:01
Which function are you asking about? The second function just turns an array into a Laravel collection. It doesn't seem very useful though since collect() already does this.
– Devon
Nov 14 '18 at 21:01
Which function are you asking about? The second function just turns an array into a Laravel collection. It doesn't seem very useful though since collect() already does this.
– Devon
Nov 14 '18 at 21:01
The checks for
!is_null($list)
, is_array($list)
, and count($list) > 0
are all unnecessary in the second function. The parameter definition doesn't allow nulls or non-arrays to be passed, and you can foreach over an empty array with no errors.– Alex Howansky
Nov 14 '18 at 21:02
The checks for
!is_null($list)
, is_array($list)
, and count($list) > 0
are all unnecessary in the second function. The parameter definition doesn't allow nulls or non-arrays to be passed, and you can foreach over an empty array with no errors.– Alex Howansky
Nov 14 '18 at 21:02
Not to mention that an array can simply be passed to
collect()
to create the collection, so the whole function is pretty useless.– miken32
Nov 14 '18 at 21:26
Not to mention that an array can simply be passed to
collect()
to create the collection, so the whole function is pretty useless.– miken32
Nov 14 '18 at 21:26
add a comment |
1 Answer
1
active
oldest
votes
Here's what happens when you run getEventsOfTheCampaigns()
step-by-step:
$campaigns
is passed first toUtility::getIndexFromCollection()
. You didn't post the code, so I'm not sure what that function does, but based on what comes next, I'm assuming it returns an array of event IDs.The result of (1) is passed to
array_unique()
. That would filter out any duplicate values in the array: http://php.net/manual/en/function.array-unique.phpThe filtered array from (2) is passed as the 2nd parameter to
array_map()
which iterates over the array, performs the given callback function on each element, and returns a new array with values that result from the callback. The first parameter toarray_map()
is the callback function which, in this case, returnsEvents::find($id)
. So the result would be an array containing all the events that it found. http://php.net/manual/en/function.array-map.phpThe array of events from (3) is then passed to
Utility::arrayToCollection()
, and that function looks like it takes the array and puts all the elements into a collection.The collection of events is returned.
But the function could make better use of Laravel's collection methods. You can turn an array into a collection by passing it to the collect()
function:
https://laravel.com/docs/5.7/collections
For example:
private static function getEventsOfTheCampaigns($campaigns)
{
// if $campaigns is not a collection, you can turn it into one like this:
// $campaigns = collect($campaigns);
// pluck the event_id from $campaigns
$eventIds = $campaigns->pluck('event_id');
// this is not required for what happens next, but you can filter the IDs if you want to
// $eventIds = $eventIds->unique()->values();
// if you pass a collection or array to the `find()` method, $events will be a collection by default
$events = Events::find($eventIds);
return $events;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308605%2fphp-arraytocollection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's what happens when you run getEventsOfTheCampaigns()
step-by-step:
$campaigns
is passed first toUtility::getIndexFromCollection()
. You didn't post the code, so I'm not sure what that function does, but based on what comes next, I'm assuming it returns an array of event IDs.The result of (1) is passed to
array_unique()
. That would filter out any duplicate values in the array: http://php.net/manual/en/function.array-unique.phpThe filtered array from (2) is passed as the 2nd parameter to
array_map()
which iterates over the array, performs the given callback function on each element, and returns a new array with values that result from the callback. The first parameter toarray_map()
is the callback function which, in this case, returnsEvents::find($id)
. So the result would be an array containing all the events that it found. http://php.net/manual/en/function.array-map.phpThe array of events from (3) is then passed to
Utility::arrayToCollection()
, and that function looks like it takes the array and puts all the elements into a collection.The collection of events is returned.
But the function could make better use of Laravel's collection methods. You can turn an array into a collection by passing it to the collect()
function:
https://laravel.com/docs/5.7/collections
For example:
private static function getEventsOfTheCampaigns($campaigns)
{
// if $campaigns is not a collection, you can turn it into one like this:
// $campaigns = collect($campaigns);
// pluck the event_id from $campaigns
$eventIds = $campaigns->pluck('event_id');
// this is not required for what happens next, but you can filter the IDs if you want to
// $eventIds = $eventIds->unique()->values();
// if you pass a collection or array to the `find()` method, $events will be a collection by default
$events = Events::find($eventIds);
return $events;
}
add a comment |
Here's what happens when you run getEventsOfTheCampaigns()
step-by-step:
$campaigns
is passed first toUtility::getIndexFromCollection()
. You didn't post the code, so I'm not sure what that function does, but based on what comes next, I'm assuming it returns an array of event IDs.The result of (1) is passed to
array_unique()
. That would filter out any duplicate values in the array: http://php.net/manual/en/function.array-unique.phpThe filtered array from (2) is passed as the 2nd parameter to
array_map()
which iterates over the array, performs the given callback function on each element, and returns a new array with values that result from the callback. The first parameter toarray_map()
is the callback function which, in this case, returnsEvents::find($id)
. So the result would be an array containing all the events that it found. http://php.net/manual/en/function.array-map.phpThe array of events from (3) is then passed to
Utility::arrayToCollection()
, and that function looks like it takes the array and puts all the elements into a collection.The collection of events is returned.
But the function could make better use of Laravel's collection methods. You can turn an array into a collection by passing it to the collect()
function:
https://laravel.com/docs/5.7/collections
For example:
private static function getEventsOfTheCampaigns($campaigns)
{
// if $campaigns is not a collection, you can turn it into one like this:
// $campaigns = collect($campaigns);
// pluck the event_id from $campaigns
$eventIds = $campaigns->pluck('event_id');
// this is not required for what happens next, but you can filter the IDs if you want to
// $eventIds = $eventIds->unique()->values();
// if you pass a collection or array to the `find()` method, $events will be a collection by default
$events = Events::find($eventIds);
return $events;
}
add a comment |
Here's what happens when you run getEventsOfTheCampaigns()
step-by-step:
$campaigns
is passed first toUtility::getIndexFromCollection()
. You didn't post the code, so I'm not sure what that function does, but based on what comes next, I'm assuming it returns an array of event IDs.The result of (1) is passed to
array_unique()
. That would filter out any duplicate values in the array: http://php.net/manual/en/function.array-unique.phpThe filtered array from (2) is passed as the 2nd parameter to
array_map()
which iterates over the array, performs the given callback function on each element, and returns a new array with values that result from the callback. The first parameter toarray_map()
is the callback function which, in this case, returnsEvents::find($id)
. So the result would be an array containing all the events that it found. http://php.net/manual/en/function.array-map.phpThe array of events from (3) is then passed to
Utility::arrayToCollection()
, and that function looks like it takes the array and puts all the elements into a collection.The collection of events is returned.
But the function could make better use of Laravel's collection methods. You can turn an array into a collection by passing it to the collect()
function:
https://laravel.com/docs/5.7/collections
For example:
private static function getEventsOfTheCampaigns($campaigns)
{
// if $campaigns is not a collection, you can turn it into one like this:
// $campaigns = collect($campaigns);
// pluck the event_id from $campaigns
$eventIds = $campaigns->pluck('event_id');
// this is not required for what happens next, but you can filter the IDs if you want to
// $eventIds = $eventIds->unique()->values();
// if you pass a collection or array to the `find()` method, $events will be a collection by default
$events = Events::find($eventIds);
return $events;
}
Here's what happens when you run getEventsOfTheCampaigns()
step-by-step:
$campaigns
is passed first toUtility::getIndexFromCollection()
. You didn't post the code, so I'm not sure what that function does, but based on what comes next, I'm assuming it returns an array of event IDs.The result of (1) is passed to
array_unique()
. That would filter out any duplicate values in the array: http://php.net/manual/en/function.array-unique.phpThe filtered array from (2) is passed as the 2nd parameter to
array_map()
which iterates over the array, performs the given callback function on each element, and returns a new array with values that result from the callback. The first parameter toarray_map()
is the callback function which, in this case, returnsEvents::find($id)
. So the result would be an array containing all the events that it found. http://php.net/manual/en/function.array-map.phpThe array of events from (3) is then passed to
Utility::arrayToCollection()
, and that function looks like it takes the array and puts all the elements into a collection.The collection of events is returned.
But the function could make better use of Laravel's collection methods. You can turn an array into a collection by passing it to the collect()
function:
https://laravel.com/docs/5.7/collections
For example:
private static function getEventsOfTheCampaigns($campaigns)
{
// if $campaigns is not a collection, you can turn it into one like this:
// $campaigns = collect($campaigns);
// pluck the event_id from $campaigns
$eventIds = $campaigns->pluck('event_id');
// this is not required for what happens next, but you can filter the IDs if you want to
// $eventIds = $eventIds->unique()->values();
// if you pass a collection or array to the `find()` method, $events will be a collection by default
$events = Events::find($eventIds);
return $events;
}
answered Nov 14 '18 at 22:31
newUserName02newUserName02
61859
61859
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308605%2fphp-arraytocollection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
In the first parameter it returns the result and in the second parameter it returns a list.
- can you be more precisely? Where is second parameter? I see only two functions - each with exactly one parameter– Kamil Kiełczewski
Nov 14 '18 at 21:01
Which function are you asking about? The second function just turns an array into a Laravel collection. It doesn't seem very useful though since collect() already does this.
– Devon
Nov 14 '18 at 21:01
The checks for
!is_null($list)
,is_array($list)
, andcount($list) > 0
are all unnecessary in the second function. The parameter definition doesn't allow nulls or non-arrays to be passed, and you can foreach over an empty array with no errors.– Alex Howansky
Nov 14 '18 at 21:02
Not to mention that an array can simply be passed to
collect()
to create the collection, so the whole function is pretty useless.– miken32
Nov 14 '18 at 21:26