0

I have custom array in my Yii 2 controller and I have to show in list view in view.

$arr = ['ABC.txt','DEF.txt','HKL.txt','ADS.txt','DDF.txt'];
 //this array pass from controller to view
1
  • How far have you got? What is your excat issue? We would need to see more code before we can help. Commented Jan 24, 2017 at 10:54

2 Answers 2

1

Use PHP MAGIC:

    $arr = ['ABC.txt','DEF.txt','HKL.txt','ADS.txt','DDF.txt'];
    $formattedArray = [];
    foreach($arr as $fileName) {
        $formattedArray[] = ['name' => $fileName];
    }

Define an ArrayDataProvider:

    $dataProvider = new ArrayDataProvider([
        'allModels' => $formattedArray,
    ]);

Pass it to view:

return $this->render('yourView', ['dataProvider' => $dataProvider]);

In yourview.php:

<?= 
   ListView::widget([
      'dataProvider' => $dataProvider,
   ]); 
?>
Sign up to request clarification or add additional context in comments.

1 Comment

But my array is not associative its numeric array, I need help for show this data to view
0

When you render your view, you have to send this array there:

$this->render('yourView', ['arr' => $arr]);

And then, in view, you can simply foreach this array and display in list. Also, you musy specify what is your $arr - like:

/* @var $arr Array */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.