+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018 |
+ | Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
* Provides a collection of static methods for array manipulation.
*
* @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
*/
class CRM_Utils_Array {
static $dst = NULL;
if (!$src) {
- $src = array('&', '<', '>', '\ 1');
- $dst = array('&', '<', '>', ',');
+ $src = ['&', '<', '>', '\ 1'];
+ $dst = ['&', '<', '>', ','];
}
return str_replace($src, $dst, $value);
* Array-encoded tree
*/
public function unflatten($delim, &$arr) {
- $result = array();
+ $result = [];
foreach ($arr as $key => $value) {
$path = explode($delim, $key);
$node = &$result;
while (count($path) > 1) {
$key = array_shift($path);
if (!isset($node[$key])) {
- $node[$key] = array();
+ $node[$key] = [];
}
$node = &$node[$key];
}
return $a1;
}
- $a3 = array();
+ $a3 = [];
foreach ($a1 as $key => $value) {
if (array_key_exists($key, $a2) &&
is_array($a2[$key]) && is_array($a1[$key])
public static function crmInArray($value, $params, $caseInsensitive = TRUE) {
foreach ($params as $item) {
if (is_array($item)) {
- $ret = crmInArray($value, $item, $caseInsensitive);
+ $ret = self::crmInArray($value, $item, $caseInsensitive);
}
else {
$ret = ($caseInsensitive) ? strtolower($item) == strtolower($value) : $item == $value;
$look = $reverse ? array_flip($lookup) : $lookup;
// trim lookup array, ignore . ( fix for CRM-1514 ), eg for prefix/suffix make sure Dr. and Dr both are valid
- $newLook = array();
+ $newLook = [];
foreach ($look as $k => $v) {
$newLook[trim($k, ".")] = $v;
}
* @return bool
* True if the array is empty.
*/
- public static function crmIsEmptyArray($array = array()) {
+ public static function crmIsEmptyArray($array = []) {
if (!is_array($array)) {
return TRUE;
}
* @return array
* Sorted array.
*/
- public static function asort($array = array()) {
+ public static function asort($array = []) {
$lcMessages = CRM_Utils_System::getUFLocale();
if ($lcMessages && $lcMessages != 'en_US' && class_exists('Collator')) {
public static function index($keys, $records) {
$final_key = array_pop($keys);
- $result = array();
+ $result = [];
foreach ($records as $record) {
$node = &$result;
foreach ($keys as $key) {
$keyvalue = isset($record->{$key}) ? $record->{$key} : NULL;
}
if (isset($node[$keyvalue]) && !is_array($node[$keyvalue])) {
- $node[$keyvalue] = array();
+ $node[$keyvalue] = [];
}
$node = &$node[$keyvalue];
}
* Keys are the original keys of $records; values are the $prop values.
*/
public static function collect($prop, $records) {
- $result = array();
+ $result = [];
if (is_array($records)) {
foreach ($records as $key => $record) {
if (is_object($record)) {
* @return array
* Keys are the original keys of $objects; values are the method results.
*/
- public static function collectMethod($method, $objects, $args = array()) {
- $result = array();
+ public static function collectMethod($method, $objects, $args = []) {
+ $result = [];
if (is_array($objects)) {
foreach ($objects as $key => $object) {
- $result[$key] = call_user_func_array(array($object, $method), $args);
+ $result[$key] = call_user_func_array([$object, $method], $args);
}
}
return $result;
}
// Empty string -> empty array
if ($values === '') {
- return array();
+ return [];
}
return explode($delim, trim((string) $values, $delim));
}
* {fg => blue, bg => black}
* }
*/
- public static function product($dimensions, $template = array()) {
+ public static function product($dimensions, $template = []) {
if (empty($dimensions)) {
- return array($template);
+ return [$template];
}
foreach ($dimensions as $key => $value) {
}
unset($dimensions[$key]);
- $results = array();
+ $results = [];
foreach ($firstValues as $firstValue) {
foreach (self::product($dimensions, $template) as $result) {
$result[$firstKey] = $firstValue;
* @return array
*/
public static function subset($array, $keys) {
- $result = array();
+ $result = [];
foreach ($keys as $key) {
if (isset($array[$key])) {
$result[$key] = $array[$key];
* Ex: [0 => ['key' => 'foo', 'value' => 'bar']].
*/
public static function makeNonAssociative($associative, $keyName = 'key', $valueName = 'value') {
- $output = array();
+ $output = [];
foreach ($associative as $key => $val) {
- $output[] = array($keyName => $key, $valueName => $val);
+ $output[] = [$keyName => $key, $valueName => $val];
}
return $output;
}
* @return array
*/
public static function multiArrayDiff($array1, $array2) {
- $arrayDiff = array();
+ $arrayDiff = [];
foreach ($array1 as $mKey => $mValue) {
if (array_key_exists($mKey, $array2)) {
if (is_array($mValue)) {
* @return array
*/
public static function filterColumns($matrix, $columns) {
- $newRows = array();
+ $newRows = [];
foreach ($matrix as $pos => $oldRow) {
- $newRow = array();
+ $newRow = [];
foreach ($columns as $column) {
$newRow[$column] = CRM_Utils_Array::value($column, $oldRow);
}
* @return array
*/
public static function rekey($array, $indexBy) {
- $result = array();
+ $result = [];
foreach ($array as $key => $value) {
$newKey = is_callable($indexBy) ? $indexBy($key, $value) : $value[$indexBy];
$result[$newKey] = $value;
$last = array_pop($pathParts);
foreach ($pathParts as $part) {
if (!isset($r[$part])) {
- $r[$part] = array();
+ $r[$part] = [];
}
$r = &$r[$part];
}
(count($keys) == 1 &&
(current($keys) > 1 ||
is_string(current($keys)) ||
- (current($keys) == 1 && $array[1] == 1) // handle (0 => 4), (1 => 1)
+ // handle (0 => 4), (1 => 1)
+ (current($keys) == 1 && $array[1] == 1)
)
)
) {
* @return array
*/
public static function buildTree($elements, $parentId = NULL) {
- $branch = array();
+ $branch = [];
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {