Array Functions
What Are Array Functions? Array functions are built-in PHP tools that let you manipulate, search, filter, and transform arrays without writing complex loops. Think of them as shortcuts – instead of writing a foreach loop every time you need to do something with an array, PHP gives you ready-made functions that do common tasks.
Why They Matter for WordPress Work: In MemberPress support, you're constantly dealing with arrays of user data, membership levels, transactions, and settings. Array functions let you quickly filter active members, search for specific subscriptions, extract email addresses, or transform data for reports – all without reinventing the wheel each time.
Core Categories:
1. Checking & Searching:
in_array($needle, $haystack)– Does this value exist in the array?array_key_exists($key, $array)– Does this key exist?array_search($value, $array)– Find the key for this value
2. Filtering & Selecting:
array_filter($array, $callback)– Keep only items that match your conditionarray_slice($array, $start, $length)– Get a portion of the arrayarray_column($array, $column_key)– Extract one column from multi-dimensional arrays
3. Transforming:
array_map($callback, $array)– Apply a function to every itemarray_values($array)– Get just the values (reset keys to 0,1,2…)array_keys($array)– Get just the keys
4. Combining & Modifying:
array_merge($array1, $array2)– Combine arraysarray_push($array, $value)– Add to endarray_pop($array)– Remove from end
5. Sorting:
sort()– Sort values ascendingrsort()– Sort values descendingksort()– Sort by keys ascendingusort($array, $callback)– Custom sorting logic
The Learning Strategy: Don't memorize the list. Instead, when you face a problem, ask yourself: “Am I checking for something? Filtering? Transforming? Combining?” Then look up the function in that category. With practice, the common ones become second nature.
Real-World Pattern: Most WordPress/MemberPress work follows this flow:
- Get an array of data (users, memberships, etc.)
- Filter it (only active, only expired, only a certain level)
- Transform it (get just emails, calculate totals, format for display)
- Use it (send emails, show in report, store in database)