Array object filter PHP

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array. The filter() function loops or iterate over each array element and pass each element to the callback function.

Syntax:

var newArray = array.filter(function(item)
 {
  return conditional_statement;
 });

Note: The filter() function does not change the original array.

Example 1: We create an array of “students” and call the filter() function on the array to derive the elements from the array that satisfy the given condition.

Javascript




<script>

    var obj = {

        'Students': [{

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
1
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
3
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
5
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
6
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
8
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
Filtered Array with non-zero and numeric id: 
 (4) [{…}, {…}, {…}, {…}]
Number of Invalid Entries =  5
1
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2
Filtered Array with non-zero and numeric id: 
 (4) [{…}, {…}, {…}, {…}]
Number of Invalid Entries =  5
3
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
Filtered Array with non-zero and numeric id: 
 (4) [{…}, {…}, {…}, {…}]
Number of Invalid Entries =  5
6
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2
Filtered Array with non-zero and numeric id: 
 (4) [{…}, {…}, {…}, {…}]
Number of Invalid Entries =  5
8
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)

<script>1<script>2

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
1
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2<script>6
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
5
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
6
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2    1
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
Filtered Array with non-zero and numeric id: 
 (4) [{…}, {…}, {…}, {…}]
Number of Invalid Entries =  5
1
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2    6
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
Filtered Array with non-zero and numeric id: 
 (4) [{…}, {…}, {…}, {…}]
Number of Invalid Entries =  5
6
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2var1
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

var3var4

var3var6

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
1
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2obj = {0
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
5
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
6
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2obj = {5
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
Filtered Array with non-zero and numeric id: 
 (4) [{…}, {…}, {…}, {…}]
Number of Invalid Entries =  5
1
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2        0
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
0
Filtered Array with non-zero and numeric id: 
 (4) [{…}, {…}, {…}, {…}]
Number of Invalid Entries =  5
6
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
2        5
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

var3var4

<script>1'Students'0

    'Students'2

'Students'

    var 'Students'6'Students'7 'Students'8

    var6

: [{1: [{2 : [{3

: [{4: [{5

: [{4: [{7

    : [{9

    

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
01

    

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
03

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
04

Output: After applying the filter function on the array, we get the first element of the array as output as it satisfies the given condition.

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)

Example 2: The following example shows filtering invalid entries from the array. We create an array of “id”s and call the filter() function on the array to derive the “id”s whose values are non-zero and numeric. 

Javascript




<script>

    

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
07

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
09

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
11

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
13

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
15

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
17

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
19

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
21
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
22 var4

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
25

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
21
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
28 : [{9

    'Students'0

'Students'

    

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
34

'Students'

    'Students'7

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
38

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
40
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
41

: [{1var6

        : [{2

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
46

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
48

: [{1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
50

: [{1: [{2

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
53
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
54

    : [{9

'Students'

    

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
59

'Students'

    

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
62
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
63
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
4

<script>1

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
66

'Students'

    

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
62
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
70
[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
71

'Students'

[{…}]
0
:{name: 'Raj', Age: '15', RollNumber: '123', Marks: '99'}
length
:1
[[Prototype]]
:Array(0)
04

Output: After applying filter() function on the array of size 9, we get 4 valid (non-zero and numeric) id and 5 invalid id

How to filter array of objects in PHP?

Filter PHP arrays code example This is pretty simple to do using the array_filter function. You pass in the original array as the first parameter and then a function as the second parameter. The array_filter function uses the passed function on each element in the array to determine whether it should get returned.

How to filter array of object by key in PHP?

Filtering a PHP array by keys To use the PHP array_filter() function to filter array elements by key instead of value, you can pass the ARRAY_FILTER_USE_KEY flag as the third argument to the function. This would pass the key as the only argument to the provided callback function.

Can you filter an array of objects?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

How to search for an object in an array in PHP?

array_search() function in PHP The array_search() function searches an array for a given value and returns the key. The function returns the key for val if it is found in the array. It returns FALSE if it is not found. If val is found in the array arr more than once, then the first matching key is returned.