Codeigniter 3 - Set Flashdata Array issue I need help about CodeIgniter $this-&gt;session-&gt;set_flashdata array.

I think my code is so long, is there any way to simplify my code?

Script at my views

&lt;?php 
for($i=0; $i&lt;$max; $i++) {
   if($this-&gt;session-&gt;flashdata("'warning".$i."'")) 
      echo $this-&gt;session-&gt;flashdata("'warning".$i."'");
   if($this-&gt;session-&gt;flashdata("'info".$i."'"))
      echo $this-&gt;session-&gt;flashdata("'info".$i."'");
}
?&gt;
&lt;form action="xxxx/insertMember" method="post"&gt;
   &lt;?php for($i=0; $i&lt;$max; $i++) { ?&gt;
      &lt;label&gt;Username &lt;?php echo $i; ?&gt;&lt;/label&gt;
      &lt;input type="text" placeholder="username" name="username[&lt;?php echo $i; ?&gt;]&gt;
   &lt;?php } ?&gt;
   &lt;button type="submit"&gt;Submit&lt;/button&gt;
&lt;/form&gt;


Script at my controller

...
parent::__construct();
$max = 5;

public function insertMember() {
....form was validated &amp; return true...
   $name = $this-&gt;input-&gt;post('username',TRUE);
   $a = 1;
   $b = 1;
   for($i=0; $i&lt;$max; $i++) {
      if($name[$i] != '') {
         //check username was registered on database or not.
         $doCheck[$i] = $this-&gt;model-&gt;check($name[$i]);

         //if already registered
         if(!empty($doCheck[$i])) {
            $this-&gt;session-&gt;set_flashdata("'warning".$a."'",$name[$i].' was registered');
            $a++;
         }

         //if not registered
         else {
            $this-&gt;model-&gt;insert($name[$i]);
            $this-&gt;session-&gt;set_flashdata("'warning".$b."'",$name[$i].' sucessfully registered');
            $b++;
         }

         redirect('registerpage');
}
This seems more like a code review question (like Alive to Die comments), so I'm not entirely sure what you are needing.  For the most part, your code looks fine.  I have a few things to consider, but they are mainly preferential.  

You could adjust the View output within the form.

&lt;?php 
    for($i=0; $i&lt;$max; $i++) { 
        echo '&lt;label&gt;Username ' . $i . ' &lt;/label&gt;&lt;input type="text" placeholder="username" name="username[' . $i . ']&gt;';
    }
?&gt;


In your Controller, you could also initiate $a and $b differently, as mentioned in this Stack Overflow article: Assign same value to multiple variables.

$a = $b = 1;