yiisoft / arrays Goto Github PK
View Code? Open in Web Editor NEWYii Array Helper
Home Page: https://www.yiiframework.com/
License: BSD 3-Clause "New" or "Revised" License
Yii Array Helper
Home Page: https://www.yiiframework.com/
License: BSD 3-Clause "New" or "Revised" License
Psalm version Psalm 5.23.1
ArrayableTrait.php - no copability with psalm version 5.23.1 and errorLevel=1
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:132:70 - Unable to determine the type that $definition is being assigned to (see https://psalm.dev/032)
foreach ($this->resolveFields($fields, $expand) as $field => $definition) {
The type of $definition is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:230:69
protected function resolveFields(array $fields, array $expand): array
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:133:13 - Unable to determine the type that $attribute is being assigned to (see https://psalm.dev/032)
$attribute = is_string($definition) ? $this->$definition : $definition($this, $field);
ERROR: MixedFunctionCall - vendor/yiisoft/arrays/src/ArrayableTrait.php:133:72 - Cannot call function on mixed (see https://psalm.dev/185)
$attribute = is_string($definition) ? $this->$definition : $definition($this, $field);
ERROR: MixedArgumentTypeCoercion - vendor/yiisoft/arrays/src/ArrayableTrait.php:136:66 - Argument 2 of App\Dota\Match\Form\MatchDetailsLoadForm::extractFieldsFor expects string, but parent type array-key provided (see https://psalm.dev/194)
$nestedFields = $this->extractFieldsFor($fields, $field);
The type of $field is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:230:69
protected function resolveFields(array $fields, array $expand): array
ERROR: MixedArgumentTypeCoercion - vendor/yiisoft/arrays/src/ArrayableTrait.php:137:66 - Argument 2 of App\Dota\Match\Form\MatchDetailsLoadForm::extractFieldsFor expects string, but parent type array-key provided (see https://psalm.dev/194)
$nestedExpand = $this->extractFieldsFor($expand, $field);
The type of $field is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:230:69
protected function resolveFields(array $fields, array $expand): array
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:144:13 - Unable to determine the type of this assignment (see https://psalm.dev/032)
$data[$field] = $attribute;
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:155:59 - Unable to determine the type that $field is being assigned to (see https://psalm.dev/032)
foreach (array_merge($rootFields, $rootExpand) as $field) {
The type of $field is sourced from here - vendor/vimeo/psalm/stubs/CoreGenericFunctions.phpstub:376:12
* @return array<TKey, TValue>
ERROR: MixedArgument - vendor/yiisoft/arrays/src/ArrayableTrait.php:156:34 - Argument 1 of array_key_exists cannot be mixed, expecting array-key (see https://psalm.dev/030)
if (array_key_exists($field, $array)) {
The type of $field is sourced from here - vendor/vimeo/psalm/stubs/CoreGenericFunctions.phpstub:376:12
* @return array<TKey, TValue>
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:157:17 - Unable to determine the type that $attribute is being assigned to (see https://psalm.dev/032)
$attribute = $array[$field];
The type of $attribute is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:150:44
private function filterAndExpand(array $array, array $fields = [], array $expand = []): array
ERROR: MixedArgument - vendor/yiisoft/arrays/src/ArrayableTrait.php:158:66 - Argument 2 of App\Dota\Match\Form\MatchDetailsLoadForm::extractFieldsFor cannot be mixed, expecting string (see https://psalm.dev/030)
$nestedFields = $this->extractFieldsFor($fields, $field);
The type of $field is sourced from here - vendor/vimeo/psalm/stubs/CoreGenericFunctions.phpstub:376:12
* @return array<TKey, TValue>
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:165:17 - Unable to determine the type of this assignment (see https://psalm.dev/032)
$data[$field] = $attribute;
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:184:29 - Unable to determine the type that $field is being assigned to (see https://psalm.dev/032)
foreach ($fields as $field) {
The type of $field is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:180:48
protected function extractRootFields(array $fields): array
ERROR: MixedOperand - vendor/yiisoft/arrays/src/ArrayableTrait.php:185:32 - Left operand cannot be mixed (see https://psalm.dev/059)
$result[] = strstr($field . '.', '.', true);
The type of $field is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:180:48
protected function extractRootFields(array $fields): array
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:209:29 - Unable to determine the type that $field is being assigned to (see https://psalm.dev/032)
foreach ($fields as $field) {
The type of $field is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:205:47
protected function extractFieldsFor(array $fields, string $rootField): array
ERROR: MixedArgument - vendor/yiisoft/arrays/src/ArrayableTrait.php:210:33 - Argument 1 of str_starts_with cannot be mixed, expecting string (see https://psalm.dev/030)
if (str_starts_with($field, "$rootField.")) {
The type of $field is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:205:47
protected function extractFieldsFor(array $fields, string $rootField): array
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:236:47 - Unable to determine the type that $definition is being assigned to (see https://psalm.dev/032)
foreach ($this->fields() as $field => $definition) {
The type of $definition is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:76:31
public function fields(): array
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:238:17 - Unable to determine the type that $field is being assigned to (see https://psalm.dev/032)
$field = $definition;
The type of $field is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:76:31
public function fields(): array
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:241:17 - Unable to determine the type of this assignment (see https://psalm.dev/032)
$result[$field] = $definition;
The type of $result[$field] is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:76:31
public function fields(): array
ERROR: MixedArrayOffset - vendor/yiisoft/arrays/src/ArrayableTrait.php:241:17 - Cannot access value on variable $result[$field] using mixed offset (see https://psalm.dev/031)
$result[$field] = $definition;
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:249:52 - Unable to determine the type that $definition is being assigned to (see https://psalm.dev/032)
foreach ($this->extraFields() as $field => $definition) {
The type of $definition is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:101:36
public function extraFields(): array
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:251:17 - Unable to determine the type that $field is being assigned to (see https://psalm.dev/032)
$field = $definition;
The type of $field is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:101:36
public function extraFields(): array
ERROR: MixedAssignment - vendor/yiisoft/arrays/src/ArrayableTrait.php:254:17 - Unable to determine the type of this assignment (see https://psalm.dev/032)
$result[$field] = $definition;
The type of $result[$field] is sourced from here - vendor/yiisoft/arrays/src/ArrayableTrait.php:101:36
public function extraFields(): array
ERROR: MixedArrayOffset - vendor/yiisoft/arrays/src/ArrayableTrait.php:254:17 - Cannot access value on variable $result[$field] using mixed offset (see https://psalm.dev/031)
$result[$field] = $definition;
Would this be useful? A 4th option for ArrayHelper::index
which specifies the key should be removed from the array.
Helps avoid some duplication of data. I can do PR and tests if it's likely to be used.
$array = [
['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
['id' => '345', 'data' => 'def', 'device' => 'tablet'],
['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
];
$result = ArrayHelper::index($array, 'id');
[
'123' => ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
'345' => ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone']
]
$result = ArrayHelper::index($array, 'id', [], true);
[
'123' => ['data' => 'abc', 'device' => 'laptop'],
'345' => ['data' => 'hgi', 'device' => 'smartphone']
]
The latest commit to BaseArrayHelper::getValue()
yiisoft/yii2@e963b2a that introduced Exceptions can bring too much pain for developers during update to 2.0.11.
ArrayHelper::getValue()
just to be on the safe side.getValue()
is frequently used in different methods of Yii core. When you use some method, you will never know that execution may can cause an exception because of getValue()
. E.g.: ArrayHelper::multisort(['a', 'b', 'c'], 0)
throws exception after the commit. This case is not covered with unit tests, of course :)ArrayHelper::getValue(null, 'foo', 'bar')
- pretty expected to get bar
.Considering aforementioned, I vote for reverting of this commit.
https://github.com/yiisoft/form/runs/7676424506?check_suite_focus=true#step:8:58
STDERR:
PHP Deprecated: Return type of Yiisoft\Form\Files::offsetGet($offset)
should either be compatible with ArrayAccess::offsetGet(mixed $offset):
mixed, or the #[\ReturnTypeWillChange] attribute should be used to
temporarily suppress the notice in
/home/runner/work/form/form/vendor/yiisoft/arrays/src/ArrayAccessTrait.ph
p on line 62
https://wiki.php.net/rfc/deprecations_php_7_4
Change:
Line 183 in 33643bf
For:
if (is_array($array) && isset($array[$key])) {
return $array[$key];
}
Line 189 in 33643bf
For:
if (strpos($key, '.') !== false) {
foreach (explode('.', $key) as $part) {
if (!isset($array[$part])) {
return $default;
}
$array = $array[$part];
}
return $array;
}
Q | A |
---|---|
Version | 1.0.? |
PHP version | |
Operating system |
class Person {
private $test = 'asefasef';
public function getTest()
{
return 123123;
}
}
$testValue = \Yiisoft\Arrays\ArrayHelper::getValue($person, 'test');
use getter method, output:
123123
Fatal error: Uncaught Error: Cannot access private property Person::$test in /usr/src/myapp/vendor/yiisoft/arrays/src/ArrayHelper.php:250
Stack trace:
If u use Symfony\Component\PropertyAccess\PropertyAccess you will get output:
123123
and maybe add some configuring options like in PropertyAccess:
$propertyAccessorBuilder->enableMagicCall(); // enables magic __call
$propertyAccessorBuilder->enableMagicGet(); // enables magic __get
$propertyAccessorBuilder->enableMagicSet(); // enables magic __set
$propertyAccessorBuilder->enableMagicMethods(); // enables magic __get, __set and __call
$propertyAccessorBuilder->disableMagicCall(); // disables magic __call
$propertyAccessorBuilder->disableMagicGet(); // disables magic __get
$propertyAccessorBuilder->disableMagicSet(); // disables magic __set
$propertyAccessorBuilder->disableMagicMethods(); // disables magic __get, __set and __call
Q | A |
---|---|
Version | 1.0.? |
PHP version | 7.4 |
Operating system |
I think we need to change namespace to Yii\Arrays. That would allow us adding more classes to the package that won't be helpers.
See #27 (comment). That could be moved to MultiSort class with dividing the method into multiple ones.
How do you like the idea of extending the functionality of setValue by adding an append parameter or similar?
Example:
$arr = [
'ะฒะพั' => [
'ะผะพะน' => [
'ะบะปัั' => null,
],
],
];
$result = \Yiisoft\Arrays\ArrayHelper::getValue($arr, ['ะฒะพั', 'ะผะพะน', 'ะบะปัั'], []);
echo $result; // [];
\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['ะฒะพั', 'ะผะพะน', 'ะบะปัั'], 'lol');
\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['ะฒะพั', 'ะผะพะน', 'ะบะปัั'], 'kek');
$result = \Yiisoft\Arrays\ArrayHelper::getValue($arr, ['ะฒะพั', 'ะผะพะน', 'ะบะปัั'], []);
echo $result; // 'kek';
And adding append
\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['ะฒะพั', 'ะผะพะน', 'ะบะปัั'], 'lol', append: true);
\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['ะฒะพั', 'ะผะพะน', 'ะบะปัั'], 'kek', append: true);
$result = \Yiisoft\Arrays\ArrayHelper::getValue($arr, ['ะฒะพั', 'ะผะพะน', 'ะบะปัั'], []);
echo $result; // ['lol', 'kek'];
If the key does not exist an empty array is initialized.
Make sure docs are good.
We have very usefull methods in ArrayHelper:
/** @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object */
public static function getValue($array, $key, $default = null);
/** @param string|array|null $path the path of where do you want to write a value to `$array` */
public static function setValue(&$array, $path, $value);
The key, path arguments support array type.
But in "remove" method, support of array type of key is missing, which is not fully.
/** @param string $key key name of the array element */
public static function remove(&$array, $key, $default = null);
Please, rewrite remove method to support array type of key.
HAL draft 8 mentions that embedded information such as the information defined by Arrayable:extraFields()
must go inside a _embedded
envelope. Currently there is no way to do it on the methodyii\base\Arrayable:toArray()
so i propose to change its signature to
toArray ( array $fields = [], array $expand = [], $recursive = true, $expandEnvelope = '')
by default $expandEnvelope
will be empty so it won't produce any envelope and the extra data will be attached as it is done right now.
that way the _embedded
property can be created by just invoking it like this
$model->toArray($fields, $extraFields, true, '_embedded');
The obvious bc break is the change of the signature for anyone who has extended this method.
other than that any code previously written will behave the same so it won't have any imcompatibility.
Q | A |
---|---|
Version | 1.0.? |
PHP version | |
Operating system |
Why should this require PHP ^7.2 when Yii 3 requires ^7.1? If I have only php 7.1 available (as happens in the work environment) I can't use this lib with Yii 3.
I am aware this is a fw independent lib, but certainly it will be used primarily with Yii 3.
$arr=['a.b' => ['c' => 'value1', 'd.e' => "value2"]];
ArrayHelper.getValue($arr, 'a.b.c'); //this will return value1
ArrayHelper.setValue($arr, 'a.b.c', 'newValue');
ArrayHelper.getValue($arr, 'a.b.c'); //this will still return value1
ArrayHelper.keyExists('a.b.c', $arr,); //false
ArrayHelper.remove($arr, 'a.b.c'); //this wont work but is currently worked on in #168
/*
$arr = [
'a' => [
'b' => [
'c' => 'newValue',
],
],
'a.b' => [
'c' => 'value1',
'd.e' => 'value2', // unreachable with dot syntax
]
]*/
Using the same key for any function in ArrayHelper (getValue, setValue, remove, keyExists) should result in accessing the same key.
Different behavior depending on which function you are using.
Q | A |
---|---|
Yii version | 3.0@dev |
PHP version | 7.1 |
Operating system | Debian 9 |
Maybe remove method isTraversable()?
This is only wrapper for is_iterable()
...
I have the following object of a query:
Yiisoft\ActiveRecord\Tests\Stubs\Order#1
(
[Yiisoft\ActiveRecord\BaseActiveRecord:attributes] => [
'id' => 1
'customer_id' => 1
'created_at' => 1325282384
'total' => '110'
]
[Yiisoft\ActiveRecord\BaseActiveRecord:oldAttributes] => [
'id' => 1
'customer_id' => 1
'created_at' => 1325282384
'total' => '110'
]
[Yiisoft\ActiveRecord\BaseActiveRecord:related] => [
'customer' => Yiisoft\ActiveRecord\Tests\Stubs\Customer#2
(
[status2] => null
[sumTotal] => null
[Yiisoft\ActiveRecord\BaseActiveRecord:attributes] => [
'id' => 1
'email' => '[email protected]'
'name' => 'user1'
'address' => 'address1'
'status' => 1
'profile_id' => 1
]
[Yiisoft\ActiveRecord\BaseActiveRecord:oldAttributes] => [
'id' => 1
'email' => '[email protected]'
'name' => 'user1'
'address' => 'address1'
'status' => 1
'profile_id' => 1
]
[Yiisoft\ActiveRecord\BaseActiveRecord:related] => []
[Yiisoft\ActiveRecord\BaseActiveRecord:relationsDependencies] => []
)
]
[Yiisoft\ActiveRecord\BaseActiveRecord:relationsDependencies] => []
)
With the current method I cannot recover customer, since of course it is not a property and therefore does not iterate and return null.
if (strpos($key, '.') !== false) {
foreach (explode('.', $key) as $part) {
if (is_array($array)) {
if (!array_key_exists($part, $array)) {
return $default;
}
$array = $array[$part];
} elseif (is_object($array)) {
if (!property_exists($array, $part)) {
return $default;
}
$array = $array->$part;
}
}
return $array;
}
Previous method: It allowed the object to be iterated, recovering the propierty.
if (strpos($key, '.') !== false) {
foreach (explode('.', $key) as $part) {
if (!array_key_exists($part, $array)) {
return $default;
}
$array = $array[$part];
}
return $array;
}
Off course \array_key_exist
is deprecated for object, change for isset its work.
ArrayHelper::getValue($object, 'customer.id') return 1.
Q | A |
---|---|
Version | 3.0 |
PHP version | 7.4 |
Operating system | Windows 10 |
Hi there! I wanted to propose, as an alternative, to make the third parameter (default
) of this function a callback. This would be useful if the default value is necessary, for example, to be obtained from the database or in some other difficult way. I do not suggest changing the API. I want the function to support this parameter with both value and callback. If a calback is transmitted, it will be called to get the value, otherwise it will be used as a value.
$a = ['A', 'B'];
$b = ['C', 'D', ReverseBlockMerge::class => new ReverseBlockMerge()];
$c = ['X'];
$result = ArrayHelper::merge($a, $b, $c);
['C', 'D', 'A', 'B', 'X']
['A', 'B', 'C', 'D', 'X']
Use \Closure
which returning the values for comparison purposes is fine.
But use \Closure
which returning array of keys for sorting (test) is too much.
If keys for sorting dependencied by array, then user should get this keys before run multisort.
$array = [
['name' => 'a', 'age' => 3],
['name' => 'b', 'age' => 2],
['name' => 'B', 'age' => 4],
['name' => 'A', 'age' => 1],
];
ArraySorter::multisort($array, fn ($item) => ['age', 'name'], SORT_DESC);
Now this code is working. Anonymous function for get columns for sort executed for ALL items of array, then used result only first run.
I suggest remove this opportunity. Anonymous function should use only for returning the values for comparison purpose.
Lines 210 to 212 in c3d14a3
Current implementation of getValue($array, $key)
with $key as anonymous function is not match with the phrases Retrieves the value of an array element
. basically it just like a value transformation, it can returns anything even value from outside array itself as of in example
Lines 184 to 188 in c3d14a3
the implementation should search value of an array by matcher function
if ($key instanceof Closure) {
foreach ($array as $key => $value) {
if ($key($value, $key)) {
return $value;
}
}
return $default;
}
and then, change the signature of matcher function to be function($value, $key): bool
.
Q | A |
---|---|
Version | 1.0.? |
PHP version | |
Operating system |
$arr1 = ['2019-01-25'];
$arr2 = ['2019-01-25'];
$arr3 = ['2019-01-25'];
$data = ArrayHelper::merge($arr1, $arr2, $arr3);
[
'2019-01-25'
]
[
'2019-01-25',
'2019-01-25',
'2019-01-25'
]
Q | A |
---|---|
Version | dev-master |
PHP version | 7.3 |
Operating system | Win7 |
there is some odd effect. let's see the example:
$value = ArrayHelper::getValue(
['a' => ['b' => ['c' => 'value']]],
'a.b.c'
);
// $value === 'value'
$newArray = ['a' => ['b' => ['c' => 'old']]];
ArrayHelper::setValue($newArray, 'a.b.c', 'new');
// $newArray === ['a' => ['b' => ['c' => 'old']], 'a.b.c' => 'new'];
another variant of implementation:
$value = ArrayHelper::getValue(
['a.b.c' => value'],
'a.b.c'
);
// $value === 'value'
$newArray = ['a.b.c' => 'old'];
ArrayHelper::setValue($newArray, 'a.b.c', 'new');
// $newArray === ['a.b.c' => 'old', 'a' => ['b' => ['c' => 'new']]];
as we can see - the fact result differs from expected. that's bad.
it would be good to drop the explode key by dots.
Instead of it, the composite key shoud be passed as array only:
$value = ArrayHelper::getValue(['a' => ['b' => ['c' => 'value']]], 'a.b.c' );
// $value === null;
$value = ArrayHelper::getValue(['a' => ['b' => ['c' => 'value']]], ['a', 'b', 'c'] );
// $value = 'value';
$value = ArrayHelper::getValue(['a' => ['b' => ['c' => 'value']]], explode('.', 'a.b.c'));
// $value = 'value';
$array = ['a' => ['b' => ['c' => 'value']]];
ArrayHelper::setValue($array, 'a.b.c', 'new' );
// $array === ['a' => ['b' => ['c' => 'value']], 'a.b.c' => 'new'];
$array = ['a' => ['b' => ['c' => 'value']]];
ArrayHelper::setValue($array, ['a', 'b', 'c'], 'new' );
// $array === ['a' => ['b' => ['c' => 'new']]];
$array = ['a' => ['b' => ['c' => 'value']]];
ArrayHelper::setValue($array, explode('.', 'a.b.c'), 'new');
// $array === ['a' => ['b' => ['c' => 'new']]];
so, the behavior is fixed and predictable, no strange effects.
in case of using objects, dots in keys are bad, but anyway, it's possible to use any keys such as '!s,o$m#e 1key',
and there is no big difference.
Q | A |
---|---|
Version | dev |
PHP version | any |
Operating system | any |
class Test implements ArrayableInterface
{
use ArrayableTrait;
public array $a = [
'b' => 1,
'c' => 2,
];
}
$object = new Test();
$object->toArray(['a.b'])
[
'a' => [
'b' => 1,
],
]
[
'a' => [
'b' => 1,
'c' => 2,
],
]
ArrayableTrait depends on Linkable
and Link
from yii-web for toArray()
:
public function toArray(array $fields = [], array $expand = [], $recursive = true)
{
$data = [];
foreach ($this->resolveFields($fields, $expand) as $field => $definition) {
$attribute = is_string($definition) ? $this->$definition : $definition($this, $field);
if ($recursive) {
$nestedFields = $this->extractFieldsFor($fields, $field);
$nestedExpand = $this->extractFieldsFor($expand, $field);
if ($attribute instanceof Arrayable) {
$attribute = $attribute->toArray($nestedFields, $nestedExpand);
} elseif (is_array($attribute)) {
$attribute = array_map(
function ($item) use ($nestedFields, $nestedExpand) {
if ($item instanceof Arrayable) {
return $item->toArray($nestedFields, $nestedExpand);
}
return $item;
},
$attribute
);
}
}
$data[$field] = $attribute;
}
if ($this instanceof Linkable) {
$data['_links'] = Link::serialize($this->getLinks());
}
return $recursive ? ArrayHelper::toArray($data) : $data;
}
As far as I know, this is a hack only used by REST, and could be moved to \yii\rest\Serializer::serializeModel(), as an additional $field
Am I missing something?
It is different from {@see get_object_vars()}
But it uses nothing more that get_object_vars()
now. Or am I missing something?
Functions getValue
, getValueByPath
, setValue
, setValueByPath
, remove
support key as float and cast float to string.
Function index
replace in float values ,
to .
.
This will work differently depending on PHP settings:
It can potentially cause problems in some cases with non-standard PHP settings.
Maybe remove support float keys in getValue
, getValueByPath
, setValue
, setValueByPath
, remove
and remove replace ,
to ,
for float values in index
?
methods like "map", 'index' etc. Can work with any iterable, just change signature of method.
Why you use array?
$array = [
'a' => 1
];
$key = ['a', 'b', 'c'];
$default =[
'c' => 'value'
];
$result = ArrayHelper::getValue($array, $key, $default);
// $result is "$value"
I think the result should be ['c' => 'value']
, right?
I think we would be nice to introduce method 'nest' similar to CakePHP one to ArrayHelper. It is used to create threaded data (with children nodes).
More information regarding it:
https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::nest
https://github.com/cakephp/utility/blob/master/Hash.php#L1172
Is it possible to get same response using ArrayHelper::map somehow ?
class Test implements ArrayableInterface
{
use ArrayableTrait;
public array $data = [
'a' => 1,
'b' => 2,
];
}
$object = new Test();
$object->toArray();
[
'data' => [
'a' => 1,
'b' => 2
]
]
[
'data' => []
]
I think the issue was introduced by PR #45.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.