douce1368 2011-12-20 10:02
浏览 102
已采纳

PHP:计算数组中的项目,将总数除以2,创建两个UL列表,其中包含来自数组的项目的相同数量的元素

I have an array containing data (ID numbers and data associated with them).

The number of items in the array is always variable and not known.

I want to split this array in two equal parts IF there's more than 2 items in the original array (not the slice).

Then I want to create two indipendent UL lists containing the resulting array slices items. If the total number of items in the original array is odd, the first list should carry one more item.

I came up with this, but I'm sure I'm doing it wrong... the content shown in the output is almost the same for each UL list, just reordered, plus in my case the number is odd (if I echo $items it comes up with 3.5).

  $panels = get_field('related_content');
  $items = count($panels);
  if ($items > 2) {
      $split = $items / 2;
      $firsthalf = array_slice($panels, $plit );
      $secondhalf = array_slice($panels, 0, $plit);
      echo '<div class="related-carousel"><ul>'; 
      foreach($firsthalf as $post_object) :
              printf('<li><a target="_blank" title="'.get_the_title($post_object->ID).'" href="'.get_permalink($post_object->ID).'"><span class="thumb">'.get_the_post_thumbnail($post_object->ID, 'smallest').'</span><span class="thumb-title"><h6>'.get_the_title($post_object->ID).'</h6></span></a><span>'.sg_get_the_excerpt().'</span></li>');
      endforeach;
      echo'</ul></div>';
      echo '<div class="related-carousel"><ul>'; 
     foreach($secondhalf as $post_object) :
             printf('<li><a target="_blank" title="'.get_the_title($post_object->ID).'" href="'.get_permalink($post_object->ID).'"><span class="thumb">'.get_the_post_thumbnail($post_object->ID, 'smallest').'</span><span class="thumb-title"><h6>'.get_the_title($post_object->ID).'</h6></span></a><span>'.sg_get_the_excerpt().'</span></li>');
     endforeach;
     echo'</ul></div>';
  }
  else {
        echo '<div class="related-carousel"><ul>';  
        foreach($panels as $post_object) :
                printf('<li><a target="_blank" title="'.get_the_title($post_object->ID).'" href="'.get_permalink($post_object->ID).'"><span class="thumb">'.get_the_post_thumbnail($post_object->ID, 'smallest').'</span><span class="thumb-title"><h6>'.get_the_title($post_object->ID).'</h6></span></a><span>'.sg_get_the_excerpt().'</span></li>');
        endforeach;
        echo'</ul></div>';
  }
  • 写回答

3条回答 默认 最新

  • drl959975 2011-12-20 10:16
    关注

    You need to change the argument $plit of array_slice into $split! It's always useful to turn on error reporting which helps with such errors: error_reporting(E_ALL).

    Could be you need to change your $split variable, e.g. by using ceil(), edit: look at AndVla answer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的