Answer the question
In order to leave comments, you need to log in
How to make ArrayHelper::index work?
Greetings.
There is an array:
$model =
[
['level1' => '1', 'level2' => 'a', 'level3' => 'a1'],
['level1' => '2', 'level2' = > 'b', 'level3' => 'b1'],
['level1' => '3', 'level2' => 'c', 'level3' => 'c1']
];
I do as it is written in the doc:
ArrayHelper::index($model, 'level3', function ($element) {
return $element[' level1'];
}, 'level2');
It turns out print_r:
Array
(
[a1] => Array
(
[level1] =>
[level1] => 2
[level2] => b
[level3] => b1
)
[c1] => Array
(
[level1] => 3
[level2] => c
[level3] => c1
)
)
Why doesn't hierarchy work ? What is wrong in doc www.yiiframework.com/doc-2.0/guide-helper-array.html
Answer the question
In order to leave comments, you need to log in
Perhaps you need to update the framework. The ArrayHelper::index() method has recently been updated to include a third parameter that specifies the grouping order. Previously, the method took only two parameters.
Here this moment is visible in more detail:
https://github.com/yiisoft/yii2/commit/ef8b0a2be0f...
And it will be correct to write like this, as in the documentation:
ArrayHelper::index($model, 'level3', [function ($element) {
return $element['level1'];
}, 'level2']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question