 <p><a href="https://3v4l.org/ZRenf" rel="nofollow">Online Check</a>, This is just a demo example.</p> <p>See below the real example:</p> <p>At first you need to use <code>array_search</code> for get the position of the same data, if exist then just remove it using <code>$arr[$pos] = '';</code>, and each and every time you need to import data into the new array called <code>$arr</code> and after completing fetching data you need to use a foreach loop to print them.</p> <pre><code>$arr = array(); foreach($query-&gt;result() as $row){ $pos = array_search($row-&gt;name, $arr); if($pos !== false) $arr[$pos] = ''; $arr[] = $row-&gt;name; } foreach($arr as $val){ echo $val.'&lt;br/&gt;'; } </code></pre> <p>Check this and let me know.</p>