Skip to content

Array API Functionality

Tonya Mork edited this page Mar 13, 2017 · 6 revisions

This submodule provides functionality to make your job easier when working with arrays. Less code for you is a good thing!

Dot Notation

Deeply nested arrays can be tedious to work with, as you need to add, remove, and access the different levels. Dot Notation gives you a simplified approach to filter down to where you need to work with the subject array.

The syntax is: keyLevel1.keyLevel2.keyLevel3

Notice that each key is separated by a dot (.).

Let's say you are working with a user's dataset and you need to get this person's twitter handle out of the array. You would do: array_get( $user, 'social.twitter' );

$user = array(
	'user_id'   => 504,
	'name'      => 'Bob Jones',
	'social'    => array(
		'twitter' => '@bobjones',
	),
	'languages' => array(
		'php'        => array(
			'procedural' => true,
			'oop'        => false,
		),
		'javascript' => true,
		'ruby'       => false,
	),
);

What gets returned then is '@bobjones';

Credit: Taylor Otwell brought us dot notation in the Laravel framework. Using his concept, Fulcrum adapts it to fit WordPress running on PHP 5.4 and up.

API Functions

Each of these API helpers is documented in the Wiki.

* indicates this function works with dot notation.

array_add*

array_exists

array_flatten_into_dots*

array_has*

array_prepend

array_remove*

array_set*

Selection Functions

These functions allow you to select the subset of elements you want from the subject array:

array_get*

array_get_except*

array_filter_with_keys

array_get_first_element

array_get_last_element

array_get_first_match

array_get_last_match

array_get_only*

array_pluck*

array_pull*

Escaping Functions

array_esc_attr

array_strip_tags

array_trim

Clone this wiki locally