Skip to main content

Array Functions

Array functions help you work with lists of data - like posts, categories, search results, or any collection of items. Instead of manually processing each item, these functions let you filter, transform, count, and organize your data automatically.

Why Array Functions Matter

Raw data challenges:

  • Lists like [post1, post2, post3] that need filtering by status
  • Category arrays where you only need the names: "Technology, WordPress, Tips"
  • Search results that need counting and limiting
  • Product lists that need sorting by price or popularity

What array functions solve:

  • Smart filtering: Show only published posts from a mixed list
  • Data extraction: Get just the category names from complex category objects
  • Automatic counting: Display "5 results found" without manual counting
  • Dynamic sorting: Arrange products by price, posts by date, or custom criteria

Checking and Counting

Verify what's in your arrays and count items.

count

Essential for showing result counts, checking if arrays have content, and loop limits.

count(array_or_string)

When to use:

  • Display search result counts
  • Check if arrays have content before showing
  • Set loop limits and pagination

includes

Perfect for checking permissions, validating selections, and conditional logic.

includes(value_to_find, array_to_search)

When to use:

  • Check user permissions and roles
  • Validate form selections
  • Filter content based on categories or tags

isset

Verify that object properties exist before trying to use them.

isset(object_or_array, property_name)

When to use:

  • Check if custom fields exist
  • Validate object structure
  • Prevent errors when accessing properties

Finding and Filtering

Extract specific items or properties from your arrays.

filter

Remove unwanted items based on conditions, like showing only published posts.

filter(array, (item) -> condition)

When to use:

  • Show only published posts
  • Filter products by price range
  • Display posts from specific categories

findIndex

Locate where a specific item appears in an array.

findIndex(array, (item) -> condition)

When to use:

  • Find current page in pagination
  • Locate specific user in list
  • Check position of featured content

pluck

Get just one property from each object in an array, like extracting all product names.

pluck(array_of_objects, property_name)

When to use:

  • Get list of category names
  • Extract all product prices
  • Create lists of titles or IDs

Transforming Items

Change or reorganize items in your arrays.

foreach

Apply changes to every item in an array, like adding calculated fields.

foreach(array, (item) -> new_value)

When to use:

  • Add calculated fields to objects
  • Format data for display
  • Combine multiple properties

sort

Put array items in alphabetical or numerical order.

sort(array)          // A to Z, low to high
rsort(array) // Z to A, high to low

When to use:

  • Alphabetize category lists
  • Sort prices low to high
  • Organize content chronologically

usort

Sort using your own rules, like by price, date, or custom criteria.

usort(array, (a, b) -> comparison)

Comparison rules:

  • Return negative number: a comes before b
  • Return positive number: b comes before a
  • Return zero: a and b are equal

When to use:

  • Sort products by price
  • Order posts by date
  • Rank items by custom scores

Combining and Splitting

Join arrays into text or split text into arrays.

join

Convert arrays into readable, formatted strings.

join(array, separator)

When to use:

  • Display category lists
  • Create comma-separated values
  • Build navigation breadcrumbs

split

Convert formatted strings into arrays for processing.

split(text, separator)

When to use:

  • Process comma-separated values
  • Parse user input
  • Break apart formatted strings

merge

Merge multiple objects together, useful for adding properties or combining data.

merge(object1, object2, ...)

When to use:

  • Add calculated fields to data
  • Combine user profile information
  • Merge configuration settings

Adding and Removing Items

Modify arrays by adding or removing specific items.

push / unshift

Add new items to arrays in specific positions (begining/end).

push(array, new_item)      // Add to end
unshift(array, new_item) // Add to beginning

When to use:

  • Add items to navigation menus
  • Insert default options in lists
  • Append additional data

set / unset

Modify or remove specific items using their position or property path.

set(array, path, new_value)    // Change item
unset(array, path) // Remove item

Path formats:

  • Array index: "[0]", "[1]"
  • Object property: "[0].price", "user.name"

When to use:

  • Update specific product prices
  • Remove sensitive data fields
  • Modify nested object properties

Working with Object Properties

Extract keys and values from objects for display or processing.

joinKeys / joinValues

Extract either the property names or values from objects.

joinKeys(object, separator)     // Get property names
joinValues(object, separator) // Get property values

When to use:

  • Create lists of available options
  • Display all values from settings
  • Generate form field lists