Source
* Sort an array of objects so that the objects whose $field value matches $pattern are at the top. Return the first
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
class CArrayHelper {
/**
* @var array
*/
protected static $fields;
private function __construct() {}
/**
* Copies keys according to given map for given array of the objects.
*
* @param array $array
* @param array $field_map
*
* @return array
*/
public static function copyObjectsKeys(array $array, array $field_map) {
foreach ($array as &$object) {
foreach ($field_map as $old_key => $new_key) {
$object[$new_key] = $object[$old_key];
}
}
unset($object);
return $array;
}
/**
* Get from array only values with given keys.
* If requested key is not in given array exception is thrown.
*
* @throws InvalidArgumentException
*
* @param array $array
* @param array $keys
*
* @return array
*/
public static function getByKeysStrict(array $array, array $keys) {
$result = [];
foreach ($keys as $key) {
if (!isset($array[$key])) {
throw new InvalidArgumentException(sprintf('Array does not have element with key "%1$s".', $key));
}
$result[$key] = $array[$key];
}