Array Functions Documentation
isAssocArray
Description: Check if an array is associative.
Parameters:
array(array): The array to check.
Returns:
bool: True if the array is associative, false otherwise.
Usage:
$isAssoc = isAssocArray(['a' => 1, 'b' => 2]); // True
$isAssoc = isAssocArray([1, 2, 3]); // False
arrayGet
Description: Get a value from an array or return a default.
Parameters:
array(array): The input array.key(mixed): The key to search for.default(mixed): The default value if the key is not found.
Returns:
mixed: The value or the default.
Usage:
$data = ['key' => 'value'];
$value = arrayGet(\$data, 'key', 'default'); // 'value'
$value = arrayGet(\$data, 'nonexistent', 'default'); // 'default'
arrayFirst
Description: Get the first element of an array.
Parameters:
array(array): The input array.
Returns:
mixed: The first element.
Usage:
$firstElement = arrayFirst([1, 2, 3]); // 1
arrayLast
Description: Get the last element of an array.
Parameters:
array(array): The input array.
Returns:
mixed: The last element.
Usage:
$lastElement = arrayLast([1, 2, 3]); // 3
arrayFlatten
Description: Flatten a multidimensional array.
Parameters:
array(array): The input array.
Returns:
array: The flattened array.
Usage:
$flatArray = arrayFlatten([[1, 2], [3, 4]]); // [1, 2, 3, 4]
arrayRemove
Description: Remove an element from an array by value.
Parameters:
array(array): The input array (by reference).value(mixed): The value to remove.
Usage:
$array = ['apple', 'banana', 'cherry'];
arrayRemove(\$array, 'banana'); // ['apple', 'cherry']
arrayRandom
Description: Get a random element from an array.
Parameters:
array(array): The input array.
Returns:
mixed: The random element.
Usage:
$randomElement = arrayRandom(['apple', 'banana', 'cherry']); // e.g., 'banana'
arrayToJson
Description: Convert an array to JSON.
Parameters:
array(array): The input array.
Returns:
string: The JSON representation.
Usage:
$json = arrayToJson(['key' => 'value']); // '{"key":"value"}'
jsonToArray
Description: Convert JSON to an array.
Parameters:
json(string): The input JSON string.
Returns:
array: The resulting array.
Usage:
$array = jsonToArray('{"key": "value"}'); // ['key' => 'value']