【问题标题】:undefined offset after using array_unique [duplicate]使用array_unique后未定义的偏移量[重复]
【发布时间】:2017-02-13 13:31:45
【问题描述】:

我正在使用 array_unique();独特的数组值它完美地工作。 但是,如果从偏移量中获取值,则会出现错误。任务偏移。

我使用array_unique()之前的数组;

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

它给出了结果:

数组 ( [0] => katha [1] => pooja [2] => 歌手 [3] => katha [4] => katha [5] => pooja [6] => archana [7] => 跳舞)

使用array_unique()后;

$tags_array = array_unique($tags_array);

它给出了结果:

数组([0] => katha [1] => pooja [2] => 歌手 [6] => archana [7] => 舞蹈)

我正在循环使用它:

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>';
}

报错:

严重性:通知

消息:未定义的偏移量:3

文件名:views/search.php

行号:178

如何进行串行偏移。喜欢

数组([0] => katha [1] => pooja [2] => 歌手 [3] => archana [4] => 舞蹈)

【问题讨论】:

    标签: php arrays codeigniter


    【解决方案1】:

    做到你没有更多基于序号的数组索引 而不是 for 你可以使用 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>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多