php - Extracting data from array in Yii 2
770
I'm trying to get data from DB and show it in menu.
echo NavX::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
array_map(function ($model) {
return [
'label' => $model['param2'],
'url' => ["/users/{$model['param3']}"],
];
}, Model::find()->where(['userID' => 32])->asArray()->all()),
],
]);
But unfortunately this code is wrong, error says:
Array to string conversion
Is there any other way to fix it ?
Answer
Solution:
My guess, without testing:
Answer
Solution:
One of the "Yii" way to do it will be using built-in
ArrayHelper
:Then just pass the
$items
to your view with menu.Official docs: