Menu

## ArrayTool
in package

ArrayTool.php : 18

Array function class

##### Tags

see
  https://www.kotchasan.com/

### Table of Contents

columns() : array<string|int, mixed>
  Get values from an array or object based on the specified column key.
delete() : array<string|int, mixed>
  Delete array keys specified by $ids. Preserves the keys of the original array.
extract() : mixed
  Extract keys and values from an array (supports nested arrays).
filter() : array<string|int, mixed>
  Filter array items that contain the specified search string.
get() : mixed
  Get the value from an array based on the specified key. Return the default value if the key is not found.
getFirstKey() : mixed|null
  Get the first key of an array or object. Returns null if no keys are found.
getNextKey() : mixed|false
  Retrieve the key of the element following a specified key in the array.
in_array() : bool
  Check if any value in the needle array exists in the haystack array.
insertAfter() : array<string|int, mixed>
  Insert data into an array after the specified key. If the key is not found, insert at the end of the array.
insertBefore() : array<string|int, mixed>
  Insert data into an array before the specified key. If the key is not found, insert at the end of the array.
replace() : mixed
  Replace values in an array or object with the specified replacement values.
search() : array<string|int, mixed>
  Searches an array for elements with a specific key-value pair and returns the matching elements.
shift() : array<string|int, mixed>
  Removes an element from an array by key and returns the resulting array.
sort() : array<string|int, mixed>
  Sorts an array of associative arrays by a specified key in ascending or descending order.
toString() : string
  Convert a nested array or object into a string by concatenating its values with a glue.
unserialize() : array<string|int, mixed>
  Unserialize a string and update the source array with the unserialized data.

### Methods

#### columns()

ArrayTool.php : 35

Get values from an array or object based on the specified column key.

`public static columns(mixed $source, string $column_key[, mixed $index_key = null ]) : array<string|int, mixed>`

##### Parameters

$source : mixed

  Array or object or array of objects

$column_key : string

  Name of the column to retrieve

$index_key : mixed = null

  Null to return the index of $source, string to return the index based on the specified column

##### Tags

assert

  (array((object)array('id' => 1, 'name' => 'one'), (object)array('id' => 2, 'name' => 'two'), array('id' => 3, 'name' => 'three')), 'name') [==] array(0 => 'one', 1 => 'two', 2 => 'three')

assert

  (array((object)array('id' => 1, 'name' => 'one'), (object)array('id' => 2, 'name' => 'two'), array('id' => 3, 'name' => 'three')), 'name', 'id') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

assert

  (array(array('id' => 1, 'name' => 'one'), array('id' => 2, 'name' => 'two'), array('id' => 3, 'name' => 'three')), 'name') [==] array(0 => 'one', 1 => 'two', 2 => 'three')

assert

  (array(array('id' => 1, 'name' => 'one'), array('id' => 2, 'name' => 'two'), array('id' => 3, 'name' => 'three')), 'name', 'id') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

assert

  ((object)array(array('id' => 1, 'name' => 'one'), array('id' => 2, 'name' => 'two'), array('id' => 3, 'name' => 'three')), 'name', 'id') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

##### Return values

array<string|int, mixed> —

#### delete()

ArrayTool.php : 61

Delete array keys specified by $ids. Preserves the keys of the original array.

`public static delete(array<string|int, mixed> $array, mixed $ids) : array<string|int, mixed>`

##### Parameters

$array : array<string|int, mixed>
$ids : mixed

  Items to delete. Can be a single value, comma-separated values, or an array of values. eg. 1 or '1,2,3' or [1,2,3]

##### Tags

assert

  (array(0, 1, 2, 3, 4, 5), '0,2') [==] array(1 => 1, 3 => 3, 4 => 4, 5 => 5)

assert

  (array(0, 1, 2, 3, 4, 5), array(0, 2)) [==] array(1 => 1, 3 => 3, 4 => 4, 5 => 5)

assert

  (array(0, 1, 2, 3, 4, 5), 2) [==] array(0 => 0, 1 => 1, 3 => 3, 4 => 4, 5 => 5)

assert

  (array('one' => 1, 'two' => 2, 'three' => 3), 'two') [==] array('one' => 1, 'three' => 3)

##### Return values

array<string|int, mixed> —

#### extract()

ArrayTool.php : 74

Extract keys and values from an array (supports nested arrays).

`public static extract(array<string|int, mixed> $array, array<string|int, mixed> &$keys, array<string|int, mixed> &$values) : mixed`

##### Parameters

$array : array<string|int, mixed>

  Array to extract eg. array('key1' => 'value1', 'key2' => 'value2', array('key3' => 'value3', 'key4' => 'value4'))

$keys : array<string|int, mixed>

  Reference to an array to store the keys eg. Array ( [0] => key1 [1] => key2 [2] => key3 [3] => key4 )

$values : array<string|int, mixed>

  Reference to an array to store the values eg. Array ( [0] => value1 [1] => value2 [2] => value3 [3] => value4 )

##### Return values

mixed —

#### filter()

ArrayTool.php : 96

Filter array items that contain the specified search string.

`public static filter(array<string|int, mixed> $array, string $search) : array<string|int, mixed>`

##### Parameters

$array : array<string|int, mixed>

  Array to filter

$search : string

  Search string

##### Tags

assert

  (array('one', 'One', 'two'), 'one') [==] array('one', 'One')

##### Return values

array<string|int, mixed> —

#### get()

ArrayTool.php : 123

Get the value from an array based on the specified key. Return the default value if the key is not found.

`public static get(array<string|int, mixed> $array, mixed $key[, mixed $default = '' ]) : mixed`

##### Parameters

$array : array<string|int, mixed>

  Array to retrieve the value from

$key : mixed

  Key to search for

$default : mixed = ''

  Default value to return if the key is not found

##### Tags

assert

  (array('one', 'two', 'three'), 0, '') [==] 'one'

assert

  (array('one', 'two', 'three'), 4, '') [==] ''

##### Return values

mixed —

#### getFirstKey()

ArrayTool.php : 141

Get the first key of an array or object. Returns null if no keys are found.

`public static getFirstKey(mixed $source) : mixed|null`

##### Parameters

$source : mixed

  Array or object

##### Tags

assert

  (array('one', 'two', 'three')) [==] 0

assert

  (array('one' => 1, 'two' => 2, 'three' => 3)) [==] 'one'

assert

  ((object)array('one' => 1, 'two' => 2, 'three' => 3)) [==] 'one'

assert

  ([]) [==] null

assert

  (0) [==] null

##### Return values

mixed|null —

#### getNextKey()

ArrayTool.php : 449

Retrieve the key of the element following a specified key in the array.

`public static getNextKey(array<string|int, mixed> $array, mixed $key) : mixed|false`

##### Parameters

$array : array<string|int, mixed>

  The input array.

$key : mixed

  The key to find the next key after.

##### Return values

mixed|false —

The key of the element following the specified key, or false if not found.

#### in_array()

ArrayTool.php : 425

Check if any value in the needle array exists in the haystack array.

`public static in_array(mixed $needle, array<string|int, mixed> $haystack[, bool $strict = false ]) : bool`

##### Parameters

$needle : mixed

  Value or array of values to search for

$haystack : array<string|int, mixed>

  Array to search in

$strict : bool = false

  (Optional) Perform strict comparison when checking values

##### Tags

assert

  (array('12.4'), array('1.10', 12.4, 1.13)) [==] true

assert

  (array('12.4'), array('1.10', 12.4, 1.13), true) [==] false

assert

  (array(1, 2), array('1', 2)) [==] true

assert

  (array('1', 2), array(1, 2)) [==] true

assert

  (array(1), array('1', 2), true) [==] false

assert

  (array('1'), array(1, 2), true) [==] false

assert

  (array(1, 2), array(1, 2)) [==] true

assert

  (array(2), array(1, 2, 3)) [==] true

assert

  (array(1, 2), array(3, 4)) [==] false

assert

  ([], array(3, 4)) [==] false

assert

  ([], []) [==] false

assert

  (array('q', array('p', 'h')), array(array('p', 'h'), array('p', 'r'), 'o')) [==] true

assert

  (array('r', 'h'), array(array('p', 'h'), array('p', 'r'), 'o')) [==] false

assert

  (array('f', 'i'), array(array('p', 'h'), array('p', 'r'), 'o')) [==] false

assert

  (array('o'), array(array('p', 'h'), array('p', 'r'), 'o')) [==] true

##### Return values

bool —

True if any value is found, false otherwise

#### insertAfter()

ArrayTool.php : 169

Insert data into an array after the specified key. If the key is not found, insert at the end of the array.

`public static insertAfter(array<string|int, mixed> $source, mixed $find, mixed $key, mixed $value) : array<string|int, mixed>`

##### Parameters

$source : array<string|int, mixed>

  Array to insert into

$find : mixed

  Key to search for

$key : mixed

  Key of the data to insert

$value : mixed

  Data to insert

##### Tags

assert

  (array('one' => 1, 'two' => 2), 'two', 'three', 3) [==] array('one' => 1, 'two' => 2, 'three' => 3)

assert

  (array(1 => 'one', 2 => 'two'), 1, 3, 'three') [==] array(1 => 'one', 3 => 'three', 2 => 'two')

assert

  (array(1 => 'one', 2 => 'two'), 2, 3, 'three') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

assert

  (array(1 => 'one', 2 => 'two'), 3, 3, 'three') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

assert

  (array(1 => 'one', 2 => 'two'), '1', 3, 'three') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

##### Return values

array<string|int, mixed> —

#### insertBefore()

ArrayTool.php : 204

Insert data into an array before the specified key. If the key is not found, insert at the end of the array.

`public static insertBefore(array<string|int, mixed> $source, mixed $find, mixed $key, mixed $value) : array<string|int, mixed>`

##### Parameters

$source : array<string|int, mixed>

  Array to insert into

$find : mixed

  Key to search for

$key : mixed

  Key of the data to insert

$value : mixed

  Data to insert

##### Tags

assert

  (array('one' => 1, 'three' => 3), 'three', 'two', 2) [==] array('one' => 1, 'two' => 2, 'three' => 3)

assert

  (array(1 => 'one', 3 => 'three'), 3, 2, 'two') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

assert

  (array(1 => 'one', 3 => 'three'), 2, 2, 'two') [==] array(1 => 'one', 3 => 'three', 2 => 'two')

assert

  (array(1 => 'one', 2 => 'two'), 1, 3, 'three') [==] array(3 => 'three', 1 => 'one', 2 => 'two')

assert

  (array(1 => 'one', 2 => 'two'), 2, 3, 'three') [==] array(1 => 'one', 3 => 'three', 2 => 'two')

assert

  (array(1 => 'one', 2 => 'two'), 3, 3, 'three') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

assert

  (array(1 => 'one', 2 => 'two'), '1', 3, 'three') [==] array(1 => 'one', 2 => 'two', 3 => 'three')

##### Return values

array<string|int, mixed> —

#### replace()

ArrayTool.php : 233

Replace values in an array or object with the specified replacement values.

`public static replace(mixed $source, array<string|int, mixed> $replace) : mixed`

##### Parameters

$source : mixed

  Array or object to replace values in

$replace : array<string|int, mixed>

  Associative array of replacement values (key => value)

##### Tags

assert

  (array(1 => 1, 2 => 2, 3 => 'three'), array(1 => 'one', 2 => 'two')) [==] array(1 => 'one', 2 => 'two', 3 => 'three')

assert

  ((object)array('one' => 1), array('two' => 2)) [==] (object)array('one' => 1, 'two' => 2)

assert

  ((object)array('one' => 1), (object)array('two' => 2)) [==] (object)array('one' => 1, 'two' => 2)

##### Return values

mixed —

The modified source array or object

#### search()

ArrayTool.php : 263

Searches an array for elements with a specific key-value pair and returns the matching elements.

`public static search(array<string|int, mixed> $array, string $key, mixed $search) : array<string|int, mixed>`

##### Parameters

$array : array<string|int, mixed>

  The array to search

$key : string

  The key to search for

$search : mixed

  The value to search for

##### Tags

assert

  (array(array('id' => 1, 'name' => 'one'), array('id' => 2, 'name' => 'two'), array('id' => 3, 'name' => 'one')), 'name', 'one') [==] array(0 => array('id' => 1, 'name' => 'one'), 2 => array('id' => 3, 'name' => 'one'))

assert

  (array(array('id' => 1, 'name' => 'one'), array('id' => 2, 'name' => 'two'), array('id' => 3, 'name' => 'one')), 'id', 'one') [==] []

assert

  (array((object)array('id' => 1, 'name' => 'one'), (object)array('id' => 2, 'name' => 'two'), (object)array('id' => 3, 'name' => 'one')), 'name', 'one') [==] array(0 => (object)array('id' => 1, 'name' => 'one'), 2 => (object)array('id' => 3, 'name' => 'one'))

assert

  (array((object)array('id' => 1, 'name' => 'one'), (object)array('id' => 2, 'name' => 'two'), (object)array('id' => 3, 'name' => 'one')), 'id', 'one') [==] []

##### Return values

array<string|int, mixed> —

The matching elements

#### shift()

ArrayTool.php : 290

Removes an element from an array by key and returns the resulting array.

`public static shift(array<string|int, mixed> $source, mixed $key) : array<string|int, mixed>`

##### Parameters

$source : array<string|int, mixed>

  The source array

$key : mixed

  The key of the element to remove

##### Tags

assert

  (array('one' => 1, 'two' => 2, 'three' => 3), 'two') [==] array('three' => 3)

assert

  (array('one' => 1, 'two' => 2, 'three' => 3), 1) [==] array('one' => 1, 'two' => 2, 'three' => 3)

##### Return values

array<string|int, mixed> —

The resulting array after removing the element

#### sort()

ArrayTool.php : 316

Sorts an array of associative arrays by a specified key in ascending or descending order.

`public static sort(array<string|int, mixed> $array[, string $sort_key = 'id' ][, bool $sort_desc = false ]) : array<string|int, mixed>`

##### Parameters

$array : array<string|int, mixed>

  The array to sort

$sort_key : string = 'id'

  The key to sort the array by

$sort_desc : bool = false

  Whether to sort the array in descending order

##### Tags

assert

  (array(array('id' => 2, 'value' => 'two'), array('id' => 3, 'value' => 'three'), array('id' => 1, 'value' => 'one'))) [==] array(array('id' => 1, 'value' => 'one'), array('id' => 2, 'value' => 'two'), array('id' => 3, 'value' => 'three'))

##### Return values

array<string|int, mixed> —

The sorted array

#### toString()

ArrayTool.php : 348

Convert a nested array or object into a string by concatenating its values with a glue.

`public static toString(string $glue, array<string|int, mixed>|object $source) : string`

##### Parameters

$glue : string

  The glue to join the values with

$source : array<string|int, mixed>|object

  The source array or object

##### Tags

assert

  ('|', array('a' => 'A', 'b' => array('b', 'B'), 'c' => array('c' => array('c', 'C')))) [==] "A|b|B|c|C"

assert

  ('|', (object)array('a' => 'A', 'b' => array('b', 'B'), 'c' => array('c' => array('c', 'C')))) [==] "A|b|B|c|C"

assert

  ('|', 'one') [==] 'one'

assert

  ('|', 1) [==] 1

##### Return values

string —

The concatenated string

#### unserialize()

ArrayTool.php : 381

Unserialize a string and update the source array with the unserialized data.

`public static unserialize(string $str[, array<string|int, mixed> $source = [] ][, bool $replace = true ]) : array<string|int, mixed>`

##### Parameters

$str : string

  The serialized string

$source : array<string|int, mixed> = []

  (Optional) Array to update with unserialized data

$replace : bool = true

  (Optional) Whether to replace existing values in the source array

##### Tags

assert

  ('') [==] []

assert

  (serialize(array(1, 2, 3))) [==] array(1, 2, 3)

assert

  (serialize(array(1 => 'One', 2 => 'Two', 3 => 'Three')), array(3 => 3, 4 => 'Four'), true) [==] array(3 => 'Three', 4 => 'Four', 1 => 'One', 2 => 'Two')

assert

  (serialize(array(1 => 'One', 2 => 'Two', 3 => 'Three')), array(3 => 3, 4 => 'Four'), false) [==] array(3 => 3, 4 => 'Four', 1 => 'One', 2 => 'Two')

##### Return values

array<string|int, mixed> —

The updated source array

## Search results