Slide 1

Slide 1 text

Use PHP arrays like a BOSS

Slide 2

Slide 2 text

Carl Alexander

Slide 3

Slide 3 text

@twigpress

Slide 4

Slide 4 text

carlalexander.ca

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

You use arrays a LOT with WordPress

Slide 7

Slide 7 text

You use loops a LOT with these arrays

Slide 8

Slide 8 text

Loops are easy to overuse

Slide 9

Slide 9 text

Makes your code complex

Slide 10

Slide 10 text

Makes your code hard to read

Slide 11

Slide 11 text

Makes your code hard to test

Slide 12

Slide 12 text

PHP has a ton of array functions

Slide 13

Slide 13 text

Relevant concepts

Slide 14

Slide 14 text

Array data type

Slide 15

Slide 15 text

Concept of array in computer science

Slide 16

Slide 16 text

Used to store a collection of values

Slide 17

Slide 17 text

Each value stored at a specific location

Slide 18

Slide 18 text

Retrieved using an index or key

Slide 19

Slide 19 text

Arrays are also used in mathematics

Slide 20

Slide 20 text

with mathematical functions

Slide 21

Slide 21 text

Functional programming

Slide 22

Slide 22 text

Important with PHP array functions

Slide 23

Slide 23 text

Revolves around functions

Slide 24

Slide 24 text

Mathematical ones, not PHP ones

Slide 25

Slide 25 text

What’s special about these functions?

Slide 26

Slide 26 text

They always take an input

Slide 27

Slide 27 text

They always return a value

Slide 28

Slide 28 text

They also come in two categories

Slide 29

Slide 29 text

Higher-order functions

Slide 30

Slide 30 text

Accept functions as arguments

Slide 31

Slide 31 text

Anonymous functions

Slide 32

Slide 32 text

Serve higher-order functions

Slide 33

Slide 33 text

Same as a string or array data type

Slide 34

Slide 34 text

PHP callables

Slide 35

Slide 35 text

Defines a function or method call

Slide 36

Slide 36 text

Also known as a callback

Slide 37

Slide 37 text

They have three different forms

Slide 38

Slide 38 text

Array and string: norm with WordPress

Slide 39

Slide 39 text

Anonymous function: new with PHP 5.3

Slide 40

Slide 40 text

Same type of functions as earlier

Slide 41

Slide 41 text

We can do functional programming!

Slide 42

Slide 42 text

This is what we’ll use to replace loops

Slide 43

Slide 43 text

You can use the other forms in your code

Slide 44

Slide 44 text

Questions?

Slide 45

Slide 45 text

Replacing loops

Slide 46

Slide 46 text

When can you use array functions?

Slide 47

Slide 47 text

Not a definitive list

Slide 48

Slide 48 text

Just a starting point

Slide 49

Slide 49 text

Validating array values

Slide 50

Slide 50 text

Using a loop to remove array values

Slide 51

Slide 51 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key') ); $posts_with_valid_meta_key = array(); foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { $posts_with_valid_meta_key[] = $post; } }

Slide 52

Slide 52 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key') ); $posts_with_valid_meta_key = array(); foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { $posts_with_valid_meta_key[] = $post; } }

Slide 53

Slide 53 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key') ); $posts_with_valid_meta_key = array(); foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { $posts_with_valid_meta_key[] = $post; } }

Slide 54

Slide 54 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key') ); $posts_with_valid_meta_key = array(); foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { $posts_with_valid_meta_key[] = $post; } }

Slide 55

Slide 55 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key') ); $posts_with_valid_meta_key = array(); foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { $posts_with_valid_meta_key[] = $post; } }

Slide 56

Slide 56 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key') ); $posts_with_valid_meta_key = array(); foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { $posts_with_valid_meta_key[] = $post; } }

Slide 57

Slide 57 text

array_filter function

Slide 58

Slide 58 text

Filters the values inside an array

Slide 59

Slide 59 text

You pass it an array and a callable

Slide 60

Slide 60 text

Iterates through each array value

Slide 61

Slide 61 text

Callable decides if value is kept or not

Slide 62

Slide 62 text

Returns filtered array

Slide 63

Slide 63 text

Replacing loop with array_filter

Slide 64

Slide 64 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key') ); $posts_with_valid_meta_key = array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } );

Slide 65

Slide 65 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key’ )); $posts_with_valid_meta_key = array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } );

Slide 66

Slide 66 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key’ )); $posts_with_valid_meta_key = array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } );

Slide 67

Slide 67 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key’ )); $posts_with_valid_meta_key = array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } );

Slide 68

Slide 68 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key’ )); $posts_with_valid_meta_key = array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } );

Slide 69

Slide 69 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key’ )); $posts_with_valid_meta_key = array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } );

Slide 70

Slide 70 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key’ )); $posts_with_valid_meta_key = array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } );

Slide 71

Slide 71 text

Questions?

Slide 72

Slide 72 text

Generating new arrays

Slide 73

Slide 73 text

Using a loop to create a new array

Slide 74

Slide 74 text

$recent_posts = get_posts(); $recent_post_permalinks = array(); foreach ($recent_posts as $post) { $recent_post_permalinks[] = get_post_permalink($post->ID); }

Slide 75

Slide 75 text

$recent_posts = get_posts(); $recent_post_permalinks = array(); foreach ($recent_posts as $post) { $recent_post_permalinks[] = get_post_permalink($post->ID); }

Slide 76

Slide 76 text

$recent_posts = get_posts(); $recent_post_permalinks = array(); foreach ($recent_posts as $post) { $recent_post_permalinks[] = get_post_permalink($post->ID); }

Slide 77

Slide 77 text

$recent_posts = get_posts(); $recent_post_permalinks = array(); foreach ($recent_posts as $post) { $recent_post_permalinks[] = get_post_permalink($post->ID); }

Slide 78

Slide 78 text

$recent_posts = get_posts(); $recent_post_permalinks = array(); foreach ($recent_posts as $post) { $recent_post_permalinks[] = get_post_permalink($post->ID); }

Slide 79

Slide 79 text

array_map function

Slide 80

Slide 80 text

Modifies an array using a callable

Slide 81

Slide 81 text

Each array value passed to callable

Slide 82

Slide 82 text

Callable applies changes to it

Slide 83

Slide 83 text

Returns transformed array

Slide 84

Slide 84 text

Replacing loop with array_map

Slide 85

Slide 85 text

$recent_posts = get_posts(); $recent_post_permalinks = array_map( function(WP_Post $post) { return get_post_permalink($post->ID); }, $recent_posts );

Slide 86

Slide 86 text

$recent_posts = get_posts(); $recent_post_permalinks = array_map( function(WP_Post $post) { return get_post_permalink($post->ID); }, $recent_posts );

Slide 87

Slide 87 text

$recent_posts = get_posts(); $recent_post_permalinks = array_map( function(WP_Post $post) { return get_post_permalink($post->ID); }, $recent_posts );

Slide 88

Slide 88 text

$recent_posts = get_posts(); $recent_post_permalinks = array_map( function(WP_Post $post) { return get_post_permalink($post->ID); }, $recent_posts );

Slide 89

Slide 89 text

$recent_posts = get_posts(); $recent_post_permalinks = array_map( function(WP_Post $post) { return get_post_permalink($post->ID); }, $recent_posts );

Slide 90

Slide 90 text

$recent_posts = get_posts(); $recent_post_permalinks = array_map( function(WP_Post $post) { return get_post_permalink($post->ID); }, $recent_posts );

Slide 91

Slide 91 text

$recent_posts = get_posts(); $recent_post_permalinks = array_map( function(WP_Post $post) { return get_post_permalink($post->ID); }, $recent_posts );

Slide 92

Slide 92 text

Transforming array of posts into links

Slide 93

Slide 93 text

Powerful concept

Slide 94

Slide 94 text

Questions?

Slide 95

Slide 95 text

Searching an array

Slide 96

Slide 96 text

Using a loop to get an array value

Slide 97

Slide 97 text

$recent_posts = get_posts(); $longest_recent_post = null; foreach ($recent_posts as $post) { if (!$longest_recent_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_recent_post->post_content) ) { $longest_recent_post = $post; } }

Slide 98

Slide 98 text

$recent_posts = get_posts(); $longest_recent_post = null; foreach ($recent_posts as $post) { if (!$longest_recent_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_recent_post->post_content) ) { $longest_recent_post = $post; } }

Slide 99

Slide 99 text

$recent_posts = get_posts(); $longest_recent_post = null; foreach ($recent_posts as $post) { if (!$longest_recent_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_recent_post->post_content) ) { $longest_recent_post = $post; } }

Slide 100

Slide 100 text

$recent_posts = get_posts(); $longest_recent_post = null; foreach ($recent_posts as $post) { if (!$longest_recent_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_recent_post->post_content) ) { $longest_recent_post = $post; } }

Slide 101

Slide 101 text

$recent_posts = get_posts(); $longest_recent_post = null; foreach ($recent_posts as $post) { if (!$longest_recent_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_recent_post->post_content) ) { $longest_recent_post = $post; } }

Slide 102

Slide 102 text

$recent_posts = get_posts(); $longest_recent_post = null; foreach ($recent_posts as $post) { if (!$longest_recent_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_recent_post->post_content) ) { $longest_recent_post = $post; } }

Slide 103

Slide 103 text

$recent_posts = get_posts(); $longest_recent_post = null; foreach ($recent_posts as $post) { if (!$longest_recent_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_recent_post->post_content) ) { $longest_recent_post = $post; } }

Slide 104

Slide 104 text

$recent_posts = get_posts(); $longest_recent_post = null; foreach ($recent_posts as $post) { if (!$longest_recent_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_recent_post->post_content) ) { $longest_recent_post = $post; } }

Slide 105

Slide 105 text

array_reduce function

Slide 106

Slide 106 text

Shrinks an array to a single value

Slide 107

Slide 107 text

Compares array values using callable

Slide 108

Slide 108 text

More complex than earlier callable

Slide 109

Slide 109 text

Callable uses two parameters

Slide 110

Slide 110 text

Current return value

Slide 111

Slide 111 text

Current array value

Slide 112

Slide 112 text

Compare the two and return one of them

Slide 113

Slide 113 text

Replacing loop with array_reduce

Slide 114

Slide 114 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 115

Slide 115 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 116

Slide 116 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 117

Slide 117 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 118

Slide 118 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 119

Slide 119 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 120

Slide 120 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 121

Slide 121 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 122

Slide 122 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 123

Slide 123 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 124

Slide 124 text

$recent_posts = get_posts(); $longest_recent_post = array_reduce( $recent_posts, function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 125

Slide 125 text

Questions?

Slide 126

Slide 126 text

Searching for a valid array value

Slide 127

Slide 127 text

Combines two loops from earlier

Slide 128

Slide 128 text

Validates array values

Slide 129

Slide 129 text

But also finds the longest post

Slide 130

Slide 130 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = null; foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { if (!$longest_post_with_valid_meta_key instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post_with_valid_meta_key->post_content) ) { $longest_post_with_valid_meta_key = $post; } } }

Slide 131

Slide 131 text

Please don't do this loop in practice!

Slide 132

Slide 132 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = null; foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { if (!$longest_post_with_valid_meta_key instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post_with_valid_meta_key->post_content) ) { $longest_post_with_valid_meta_key = $post; } } }

Slide 133

Slide 133 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = null; foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { if (!$longest_post_with_valid_meta_key instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post_with_valid_meta_key->post_content) ) { $longest_post_with_valid_meta_key = $post; } } }

Slide 134

Slide 134 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = null; foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { if (!$longest_post_with_valid_meta_key instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post_with_valid_meta_key->post_content) ) { $longest_post_with_valid_meta_key = $post; } } }

Slide 135

Slide 135 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = null; foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { if (!$longest_post_with_valid_meta_key instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post_with_valid_meta_key->post_content) ) { $longest_post_with_valid_meta_key = $post; } } }

Slide 136

Slide 136 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = null; foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { if (!$longest_post_with_valid_meta_key instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post_with_valid_meta_key->post_content) ) { $longest_post_with_valid_meta_key = $post; } } }

Slide 137

Slide 137 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = null; foreach ($posts_with_meta_key as $post) { if (my_plugin_is_meta_key_valid($post->post_meta_key)) { if (!$longest_post_with_valid_meta_key instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post_with_valid_meta_key->post_content) ) { $longest_post_with_valid_meta_key = $post; } } }

Slide 138

Slide 138 text

Combining array functions

Slide 139

Slide 139 text

No new array function!

Slide 140

Slide 140 text

New concept: function composition

Slide 141

Slide 141 text

Used with functional programming

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No need to create large functions

Slide 144

Slide 144 text

Make functions as small as possible

Slide 145

Slide 145 text

Compose multiple functions together

Slide 146

Slide 146 text

Composing array functions

Slide 147

Slide 147 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 148

Slide 148 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 149

Slide 149 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 150

Slide 150 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 151

Slide 151 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 152

Slide 152 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 153

Slide 153 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 154

Slide 154 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 155

Slide 155 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 156

Slide 156 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 157

Slide 157 text

$posts_with_meta_key = get_posts(array( 'meta_key' => ‘post_meta_key' )); $longest_post_with_valid_meta_key = array_reduce( array_filter( $posts_with_meta_key, function(WP_Post $post) { return my_plugin_is_meta_key_valid($post->post_meta_key); } ), function($longest_post, WP_Post $post) { if (!$longest_post instanceof WP_Post || str_word_count($post->post_content) > str_word_count($longest_post->post_content) ) { return $post; } return $longest_post; } );

Slide 158

Slide 158 text

Questions?

Slide 159

Slide 159 text

Why do this?

Slide 160

Slide 160 text

Loops are familiar

Slide 161

Slide 161 text

Using array functions isn’t

Slide 162

Slide 162 text

You won’t always feel that way

Slide 163

Slide 163 text

You’ll get better with time

Slide 164

Slide 164 text

Reduces complexity

Slide 165

Slide 165 text

Improves code quality

Slide 166

Slide 166 text

Prevents technical debt

Slide 167

Slide 167 text

Questions?

Slide 168

Slide 168 text

Thank you!