Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Use PHP arrays like a boss

Use PHP arrays like a boss

These are the slides from a talk given at WordCamp Los Angeles 2016.

You’ve heard of the good ol’ school PHP loops. You have your “for” loops, your “foreach” loops, your “while” loops, your “fruit” loops… Well maybe not that last one!

As a WordPress developer, you use arrays and loops all the time. They’re an essential part of your developer toolbox. But do you know everything about them?

PHP has a wealth of array functions. They’re used by PHP experts to make their life easier. They let you reduce the amount of PHP code you need. This helps prevent bugs and makes your code more maintainable.

You’ll learn about functions like array_filter, array_map and array_reduce. All within the context of regular WordPress work. By the end of this talk, loops won’t look at you the same way ever again.

You can read the companion article at: https://carlalexander.ca/php-array-functions-instead-loops/

Carl Alexander

September 10, 2016
Tweet

More Decks by Carl Alexander

Other Decks in Programming

Transcript

  1. $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; } }
  2. $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; } }
  3. $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; } }
  4. $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; } }
  5. $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; } }
  6. $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; } }
  7. $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); } );
  8. $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); } );
  9. $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); } );
  10. $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); } );
  11. $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); } );
  12. $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); } );
  13. $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); } );
  14. $recent_posts = get_posts(); $recent_post_permalinks = array(); foreach ($recent_posts as $post)

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

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

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

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

    { $recent_post_permalinks[] = get_post_permalink($post->ID); }
  19. $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; } }
  20. $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; } }
  21. $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; } }
  22. $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; } }
  23. $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; } }
  24. $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; } }
  25. $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; } }
  26. $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; } }
  27. $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; } );
  28. $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; } );
  29. $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; } );
  30. $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; } );
  31. $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; } );
  32. $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; } );
  33. $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; } );
  34. $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; } );
  35. $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; } );
  36. $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; } );
  37. $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; } );
  38. $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; } } }
  39. $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; } } }
  40. $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; } } }
  41. $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; } } }
  42. $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; } } }
  43. $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; } } }
  44. $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; } } }
  45. $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; } );
  46. $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; } );
  47. $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; } );
  48. $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; } );
  49. $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; } );
  50. $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; } );
  51. $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; } );
  52. $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; } );
  53. $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; } );
  54. $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; } );
  55. $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; } );