duanqiang9212 2019-05-01 10:38
浏览 48

从View中完全删除<li>标记

I have an array object from a son API and I want to display only the first 3 items and hide the rest of the items.

Unfortunately when I try to hide the rest of the items from the view it still shows that a list was there but hidden. I want to hide that completely without destroying the values in that list.

My code below

$characters = json_decode($data);
foreach ($characters as $character) {

    <?php echo "<li style='display:none'>".$character->name . '<br>'."</li>"; ?>

}
  • 写回答

1条回答 默认 最新

  • drf97973 2019-05-01 11:20
    关注

    You can use array_slice method to create another array with reduced number of items (for your example, that'd be 3).

    Solution for your Example:

    <?php echo "<li>". array_slice($character, 0, 3)->name . '<br>'."</li>"; ?>
    

    More info about array_slice method:

    array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.

    array_slice(array,start,length,preserve)
    

    array: The input array.

    offset: number

    • The offset parameter denotes the position in the array, not the key. If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

    length: number

    • If length is given and is positive, then the sequence will have up to that many elements in it.

    • If the array is shorter than the length, then only the available array elements will be present.

    • If length is given and is negative then the sequence will stop that many elements from the end of the array.

    • If it is omitted, then the sequence will have everything from offset up until the end of the array.

    preserve_keys: boolean

    • array_slice() will reorder and reset the integer array indices by default. This behavior can be changed by setting preserve_keys to TRUE. String keys are always preserved, regardless of this parameter.
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题