iflrandevu/application/helpers/array_helper.php

75 lines
1.9 KiB
PHP
Raw Normal View History

2022-10-16 18:03:55 +03:00
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.4.0
* ---------------------------------------------------------------------------- */
if (!function_exists('is_assoc')) {
2022-10-16 18:03:55 +03:00
/**
* Check if an array is an associative array.
*
* @param array $array
*
* @return bool
*/
function is_assoc(array $array): bool
{
if (empty($array)) {
return false;
2022-10-16 18:03:55 +03:00
}
return array_keys($array) !== range(0, count($array) - 1);
}
}
if (!function_exists('array_find')) {
2022-10-16 18:03:55 +03:00
/**
* Find the first array element based on the provided function.
*
* @param array $array
* @param callable $callback
*
* @return mixed
*/
function array_find(array $array, callable $callback): mixed
{
if (empty($array)) {
return null;
2022-10-16 18:03:55 +03:00
}
if (!is_callable($callback)) {
2022-10-16 18:03:55 +03:00
throw new InvalidArgumentException('No filter function provided.');
}
return array_filter($array, $callback)[0] ?? null;
2022-10-16 18:03:55 +03:00
}
}
if (!function_exists('array_fields')) {
2022-10-16 18:03:55 +03:00
/**
* Keep only the provided fields of an array.
*
* @param array $array
* @param array $fields
*
* @return array
*/
function array_fields(array $array, array $fields): array
{
return array_filter(
$array,
function ($field) use ($fields) {
return in_array($field, $fields);
},
ARRAY_FILTER_USE_KEY,
);
2022-10-16 18:03:55 +03:00
}
}