0

i am using array_unique(); to unique array values it perfectly works. but when if getting value form offset it gives error. mission offset.

my array before using array_unique();

$tags_array = explode(',', $tagss);

it gives result :

Array ( [0] => katha [1] => pooja [2] => singer [3] => katha [4] => katha [5] => pooja [6] => archana [7] => dance )

after using array_unique();

$tags_array = array_unique($tags_array);

it gives result :

Array ( [0] => katha [1] => pooja [2] => singer [6] => archana [7] => dance )

i am using it in loop:

for($i=0;$i<count($tags_array);$i++){
    echo '<a class="label label-primary tags" href="'.base_url().'home/tag/'.$tags_array[$i].'">'.ucfirst($tags_array[$i]).'</a>';
}

it gives error:

Severity: Notice

Message: Undefined offset: 3

Filename: views/search.php

Line Number: 178

how to make serial wise offset. like

Array ( [0] => katha [1] => pooja [2] => singer [3] => archana [4] => dance )

Community
  • 1
  • 1

1 Answers1

0

do the fact that you have not more the array index based on sequential number instead of for you could use foreach

foreach($tags_array as $key => $value{
   echo '<a class="label label-primary tags" href="'.
         base_url().'home/tag/'.$tags_array[$i].'">'.
             ucfirst($value).'</a>';
}
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107