Skip to content

Latest commit

 

History

History
16 lines (14 loc) · 447 Bytes

avoid_deeply_nested_loops_with_eachSpread.md

File metadata and controls

16 lines (14 loc) · 447 Bytes

Avoid deeply nested loops with eachSpread

Here's a trick for avoiding deeply nested loops with a little collection magic.. eachSpread'ing the result of a crossJoin

// Old manual iteration method
foreach ($posts as $post) {
    foreach ($tags as $tag) {
        doSomethingWith($post)->and($tag);
    }
}

// The declarative way
$posts->crossJoin($tags)->eachSpread(function ($post, $tag) {
    doSomethingWith($post)->and($tag);
});