Final Class Yiisoft\ActiveRecord\Internal\ArArrayHelper
| Inheritance | Yiisoft\ActiveRecord\Internal\ArArrayHelper |
|---|
Psalm Types
| Name | Value |
|---|---|
| Row | Yiisoft\ActiveRecord\ActiveRecordInterface|array |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| getColumn() | Returns the values of a specified column in an array. | Yiisoft\ActiveRecord\Internal\ArArrayHelper |
| getValueByPath() | Retrieves a value from the array by the given key or from the {@see ActiveRecordInterface} instance by the given property or relation name. | Yiisoft\ActiveRecord\Internal\ArArrayHelper |
| index() | Indexes an array of rows with the specified column value as keys. | Yiisoft\ActiveRecord\Internal\ArArrayHelper |
| toArray() | Converts an object into an array. | Yiisoft\ActiveRecord\Internal\ArArrayHelper |
Method Details
Returns the values of a specified column in an array.
The input array should be multidimensional or an array of {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} instances.
For example,
$array = [
['id' => '123', 'data' => 'abc'],
['id' => '345', 'data' => 'def'],
];
$result = ArArrayHelper::getColumn($array, 'id');
// the result is: ['123', '345']
| public static array getColumn ( Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] $array, string $name ) | ||
| $array | Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] |
Array to extract values from. |
| $name | string |
The column name. |
| return | array |
The list of column values. |
|---|---|---|
public static function getColumn(array $array, string $name): array
{
return array_map(
static fn(ActiveRecordInterface|array $element): mixed => self::getValueByPath($element, $name),
$array,
);
}
Retrieves a value from the array by the given key or from the {@see ActiveRecordInterface} instance by the given property or relation name.
If the key doesn't exist, the default value will be returned instead.
The key may be specified in a dot format to retrieve the value of a sub-array or a property or relation of the {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} instance.
In particular, if the key is x.y.z, then the returned value would be $array['x']['y']['z'] or
$array->x->y->z (if $array is an {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} instance).
Note that if the array already has an element x.y.z, then its value will be returned instead of going through
the sub-arrays.
Below are some usage examples.
// working with an array
$username = ArArrayHelper::getValueByPath($array, 'username');
// working with an {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} instance
$username = ArArrayHelper::getValueByPath($user, 'username');
// using dot format to retrieve the property of an {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} instance
$street = ArArrayHelper::getValue($users, 'address.street');
| public static mixed getValueByPath ( Yiisoft\ActiveRecord\ActiveRecordInterface|array $array, string $key, mixed|null $default = null ) | ||
| $array | Yiisoft\ActiveRecord\ActiveRecordInterface|array |
Array or an {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} instance to extract value from. |
| $key | string |
Key name of the array element or a property or relation name of the {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} instance. |
| $default | mixed|null |
The default value to be returned if the specified |
| return | mixed |
The value of the element if found, default value otherwise |
|---|---|---|
public static function getValueByPath(ActiveRecordInterface|array $array, string $key, mixed $default = null): mixed
{
if ($array instanceof ActiveRecordInterface) {
if ($array->hasProperty($key)) {
return $array->get($key);
}
if (property_exists($array, $key)) {
/** @psalm-suppress PossiblyNullFunctionCall */
return (Closure::bind(fn(): mixed => $this->$key, $array, $array))();
}
if ($array->isRelationPopulated($key)) {
return $array->relation($key);
}
} elseif (array_key_exists($key, $array)) {
return $array[$key];
}
if (!empty($key) && ($pos = strrpos($key, '.')) !== false) {
$array = self::getValueByPath($array, substr($key, 0, $pos), $default);
if (!is_array($array) && !($array instanceof ActiveRecordInterface)) {
throw new RuntimeException(
'Trying to get property of non-array or non-ActiveRecordInterface instance.',
);
}
$key = substr($key, $pos + 1);
return self::getValueByPath($array, $key, $default);
}
return $default;
}
Indexes an array of rows with the specified column value as keys.
The input array should be multidimensional or an array of {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} instances.
For example,
$rows = [
['id' => '123', 'data' => 'abc'],
['id' => '345', 'data' => 'def'],
];
$result = ArArrayHelper::populate($rows, 'id');
// the result is: ['123' => ['id' => '123', 'data' => 'abc'], '345' => ['id' => '345', 'data' => 'def']]
| public static Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] index ( Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] $rows, Closure|string|null $indexBy = null ) | ||
| $rows | Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] |
Array to populate. |
| $indexBy | Closure|string|null |
The column name or anonymous function that specifies the index by which to populate the array of rows. |
public static function index(array $rows, Closure|string|null $indexBy = null): array
{
if ($indexBy === null) {
return $rows;
}
if ($indexBy instanceof Closure) {
return array_combine(array_map($indexBy, $rows), $rows);
}
$result = [];
foreach ($rows as $row) {
$result[(string) self::getValueByPath($row, $indexBy)] = $row;
}
return $result;
}
Converts an object into an array.
| public static array toArray ( array|object $object ) | ||
| $object | array|object |
The object to be converted into an array. |
| return | array |
The array representation of the object. |
|---|---|---|
public static function toArray(array|object $object): array
{
if (is_array($object)) {
return $object;
}
if ($object instanceof ActiveRecordInterface) {
return $object->propertyValues();
}
if ($object instanceof Traversable) {
/**
* @psalm-var array<string, mixed> We assume that the traversable object yields string keys.
*/
return iterator_to_array($object);
}
return get_object_vars($object);
}
Signup or Login in order to comment.