Array Helper

The array helper provides several functions to simplify more complex usages of arrays. It is not intended to duplicateany of the existing functionality that PHP provides - unless it is to vastly simplify their usage.

Loading this Helper

This helper is loaded using the following code:

  1. helper('array');

Available Functions

The following functions are available:

  • dotarray_search(_string $search, array $values)

Parameters:

  • $search (string) – The dot-notation string describing how to search the array
  • $values (array) – The array to searchReturns:The value found within the array, or nullReturn type:mixed

This method allows you to use dot-notation to search through an array for a specific-key,and allows the use of a the ‘*’ wildcard. Given the following array:

  1. $data = [
  2. 'foo' => [
  3. 'buzz' => [
  4. 'fizz' => 11
  5. ],
  6. 'bar' => [
  7. 'baz' => 23
  8. ]
  9. ]
  10. ]

We can locate the value of ‘fizz’ by using the search string “foo.buzz.fizz”. Likewise, the valueof baz can be found with “foo.bar.baz”:

  1. // Returns: 11
  2. $fizz = dot_array_search('foo.buzz.fizz', $data);
  3.  
  4. // Returns: 23
  5. $baz = dot_array_search('foo.bar.baz', $data);

You can use the asterisk as a wildcard to replace any of the segments. When found, it will search through allof the child nodes until it finds it. This is handy if you don’t know the values, or if your valueshave a numeric index:

  1. // Returns: 23
  2. $baz = dot_array_search('foo.*.baz', $data);