Skip to content

Array Functions

Before you begin

In order to use the features in this section you need to have an active Spojit account. If you don't have an account you can checkout out the pricing and register here. If you already have an account you can login here.


Count

count(array $value):int
Counts all elements in given array and returns a number (integer).
  count(["one", "two", "three"]) => 3
  count([1, 2]) => 2
  count([0]) => 1
  count([]) => 0


Get Item By Index

getItemByIndex(array $value, integer $index):mixed
Searches an array for given index and returns the array item found at the index specified. Array indexes start at 0 - zero index represents the first item in an array.

Throws exception if array does not contain an item at specified index.

  getItemByIndex(["one", "two", "three"], 0) => "one"
  getItemByIndex([1, 2], 1) => 2
  getItemByIndex([0], 1) => throws Exception (array does not have an item at index 1)


Remove Item By Index

removeItemByIndex(array $value, integer $index):array
Searches an array for given index and removes value or object at the specified index. Array indexes start at 0 - zero index represents the first item in an array. Will reindex the array afterwards.

Throws exception if array does not contain an item at specified index.

  removeItemByIndex(["one", "two", "three"], 0) => ["two", "three"]
  removeItemByIndex([1, 2], 1) => [2]
  removeItemByIndex([0], 1) => throws Exception (array does not have an item at index 1)


Implode (Join)

implode( array $value, string $glue):string
Joins all elements of an array. Returns a string containing a representation of all the array elements in the same order, with the glue string between each element.
  implode(["one", "two", "three"], " ") => "one two three"
  implode([1, 2, 3], ", ") => "1, 2, 3"
  implode([1, 2, 3]) => "123"


Array Filter

arr_filter(array $collection, array $conditions):array
Searches input $collection (array of objects) for items matching $conditions. Returns a new collection with records matching those $conditions.

$condition.field: the name of the object field (ie. 'id', or 'order_id'). Nested objects use dot notation (ie. 'address.street_address' would refer to a 'street_address' field in nested object 'address' in each object in collection).

$condition.value: the value being searched for. Separate multiple search values by comma.

For a $collection of person objects where each person has an 'id', a condition where $condition.field = 'id' and $condition.value = '1,2', this function will return a new array containing all persons where 'id' is either '1' or '2'. For the same $collection of persons, a condition where $condition.field = 'contact.email' and $condition.value = 'john-at-email-dot-com', this function will return a new array containing all persons where contact email address is 'john-at-email-dot-com'.

Returns an empty array [ ] if no matching items are found.

  Given the following $collection:

  [
    {"id": 1, "name": "John", "contact": {"email": "john@email.com"}},
    {"id": 2, "name": "Mary", "contact": {"email": "mary@email.com"}},
    {"id": 3, "name": "Flynn", "contact": {"email": "flynn@email.com"}},
    {"id": 4, "name": "Brad", "contact": {"email": "brad@email.com"}}
  ]

  //Find one by 'id' where id equals 2
  $condition = [{$field = "id", $value = "2"}]
  arr_filter($collection, $conditions) => [{"id": 2, "name": "Mary", "contact": {"email": "mary@email.com"}}}]

  //Find one by 'id' where id equals 10 ($field = "id", $value = "10")
  $conditions = [{$field = "id", $value = "10"}]
  arr_filter($collection, $conditions) => [] //no item where id=10 exists - returns empty array

  //Find one by contact email where email equals "brad@email.com"
  $conditions = [{$field = "contact.email", $value = "brad@email.com"}]
  arr_filter($collection, $conditions) => [{"id": 4, "name": "Brad", "contact": {"email": "brad@email.com"}}]

  //Find any by 'id' where id equals 2 or 4
  $conditions = [{$field = "id", $value = "2,4"}]
  arr_filter($collection, $conditions) => [{"id": 2, "name": "Mary", "contact": {"email": "mary@email.com"}},{"id": 4, "name": "Brad", "contact": {"email": "brad@email.com"}}]

  //Find all where name is Flynn or id is 1 (two conditions required)
  $conditions = [{$field = "name", $value = "Flynn"}, {$field = "id", $value = "1"}]
  arr_filter($collection, $conditions) => [{"id": 1, "name": "John", "contact": {"email": "john@email.com"}},{"id": 3, "name": "Flynn", "contact": {"email": "flynn@email.com"}}]

Array Map

arr_map(array $collection, string $field):array
Searches input $collection (array of objects) for values matching the provided $field. Returns a new collection of values matching that $field.

$field: the name of the object field (ie. 'id', or 'order_id').

For a $collection of person objects where each person has a 'name' and a field where $field = 'name' this function will return a new array containing all persons names.

Returns an empty array [ ] if no matching items are found.

  Given the following $collection:

  [
    {"id": 1, "name": "John", "contact": {"email": "john@email.com"}},
    {"id": 2, "name": "Mary", "contact": {"email": "mary@email.com"}},
    {"id": 3, "name": "Flynn", "contact": {"email": "flynn@email.com"}},
    {"id": 4, "name": "Brad", "contact": {"email": "brad@email.com"}}
  ]

  //Find all values where field equals name
  $field = "name"
  arr_map($collection, $field) => ["John", "Mary", "Flynn", "Brad"]

  //Find all values where field equals id
  $field = "id"
  arr_map($collection, $field) => [1, 2, 3, 4]

  //Find all values where field equals contact
  $field = "contact"
  arr_map($collection, $field) => [{"email": "john@email.com"}, {"email": "mary@email.com"}, {"email": "flynn@email.com"}, {"email": "brad@email.com"}]

Array Reduce

arr_reduce(string $type, string $method, string $field, array $collection):mixed
Iterates over elements of a $collection, reducing the array to a single value/object using a predetermined $method.

$type: the type of array (ie. 'simple', or 'object').

$method: the method to reduce the array (ie. 'max', or 'min').

$field: the name of the field if object type is used (ie. 'id', or 'order_id').

  Given the following $collection:

  [
    {"id": 1, "name": "John", "contact": {"email": "john@email.com"}},
    {"id": 2, "name": "Mary", "contact": {"email": "mary@email.com"}},
    {"id": 3, "name": "Flynn", "contact": {"email": "flynn@email.com"}},
    {"id": 4, "name": "Brad", "contact": {"email": "brad@email.com"}}
  ]

  //Reduce the array using the max method on an array of objects
  arr_reduce('object', 'max' 'id', $collection) => {"id": 4, "name": "Brad", "contact": {"email": "brad@email.com"}}

  Given the following $collection:

  [54, 4, 19, 5, 16, 6]

  //Reduce the array using the min method on a simple array
  arr_reduce('simple', 'min' null, $collection) => 4

Add Index To Object

add_index_to_object(string $field, array $collection):array
Iterates over elements of a $collection, adding the collection index as a $field on the object.

$field: the name of the field on the object (ie. 'sequence', or 'order_id').

  Given the following $collection:

  [
    {"id": 1, "name": "John", "contact": {"email": "john@email.com"}},
    {"id": 2, "name": "Mary", "contact": {"email": "mary@email.com"}},
    {"id": 3, "name": "Flynn", "contact": {"email": "flynn@email.com"}},
    {"id": 4, "name": "Brad", "contact": {"email": "brad@email.com"}}
  ]

  //Add an index on the 'index_value' on an array of objects
  add_index_to_object('index_value', $collection) => [{"index_value":0,"id":1,"name":"John","contact":{"email":"john@email.com"}},{"index_value":1,"id":2,"name":"Mary","contact":{"email":"mary@email.com"}},{"index_value":2,"id":3,"name":"Flynn","contact":{"email":"flynn@email.com"}},{"index_value":3,"id":4,"name":"Brad","contact":{"email":"brad@email.com"}}]
Back to top