Search

Laravel Array Helper Function: array_where

November 20, 2016 —John Koster

where($array, callable $callback)

The where helper method is functionally similar to the pluck method, in that both take a source $array and filter it to produce a smaller array. It works by iterating over each element in the $array and executing the $callback on each key value pair. If the $callback returns a value that evaluates to true, the key/value pair is kept, otherwise it is ignored when constructing the final array.

#Comparing Just the Value

Assume we have an array of numbers, from 0 to 100:

1<?php
2 
3$numbers = range(0, 100);

An array of all numbers less than or equal to 10 can be created like so:

1<?php
2 
3use Illuminate\Support\Arr;
4 
5$smallNumbers = Arr::where($numbers, function($key, $value)
6{
7 return ($value <= 10);
8});

$smallNumbers would then look like this:

1array(11) {
2 [0] 0
3 [1] 1
4 [2] 2
5 [3] 3
6 [4] 4
7 [5] 5
8 [6] 6
9 [7] 7
10 [8] 8
11 [9] 9
12 [10] 10
13}

#Comparing Both Key and Value

That example only used the $value from the array, the following example will make use of both the key and value.

Consider the following array of students and test scores:

1<?php
2 
3$testScores = [
4 'Alice' => 95,
5 'Allison' => 96,
6 'Bob' => 92,
7 'Delbert' => 72,
8 'Dennis' => 87,
9 'Tonya' => 32
10];

Let's assume that any test score below a 70 is a failing grade. What we want to do is get an array of everyone who has passed the test, but whose name starts with the letter a:

1<?php
2 
3use Illuminate\Support\Arr;
4use Illuminate\Support\Str;
5 
6$studentsWhoPassed = Arr::where($testScores, function($student, $testScore)
7{
8 return (Str::startsWith(Str::upper($student), 'A') && $testScore >= 70);
9});

The value of $studentsWhoPassed would be:

1array(2) {
2 ["Alice"] 95
3 ["Allison"] 96
4}

#Comparing Just the Key

For completeness, let's look at an example of just utilizing the key from the key/value compare with the where method.

Let's add another array to the mix to keep track of all the students that are currently suspended:

1<?php
2 
3$suspendedStudents = [
4 'Dennis',
5 'Tonya'
6];

Now we want to get only the test scores of those students who are not currently suspended (students who are suspended to not get their test scores credited, and instead receive a 0 mark):

1<?php
2 
3$validTestScores = Arr::where($testScores,
4 function($student, $testScore)
5 use ($suspendedPeople)
6{
7 return (!Arr::has(array_flip($suspendedPeople), $student));
8});
Anonymous functions and variable scope

In PHP, whenever the execution of the script enters into a function, the scope changes. This is still true for Closures and anonymous functions. That is why the above example is using the use keyword: to bring the $suspendedPeople variable from another scope into the current scope.

It should be noted that when variables are "pulled into" the current scope, the current scope receives a copy of that variable. That is why the call to array_flip does not change the original array.

The value of $validTestScores would then be:

1array(4) {
2 ["Alice"] 95
3 ["Allison"] 96
4 ["Bob"] 92
5 ["Delbert"] 72
6}

#array_where($array, callable $callback)

The array_where function is a shortcut to calling Arr::where. This function is declared in the global namespace.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.