PHP:计数数组中的项目,将总数除以2,创建两个UL列表,其中包含来自数组的包含项目的元素数量相等 [英] PHP: Count items in array, split total by two, create two UL lists with equal number of elements containing items from array

查看:120
本文介绍了PHP:计数数组中的项目,将总数除以2,创建两个UL列表,其中包含来自数组的包含项目的元素数量相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数据的数组(ID号和与之关联的数据)。



数组中的项数总是变量, p>

我想把这个数组分成两个相等的部分,如果原始数组中有超过2个项目(而不是切片)。



然后,我想创建两个独立的UL列表,其中包含所得到的数组切片项目。如果原始数组中的项目总数是奇数,第一个列表应该再携带一个项目。



我想出了这个,但我相信我' m做错了...输出中显示的内容对于每个UL列表几乎是相同的,只是重新排序,加上在我的情况下,数字是奇数(如果我回声$ items它出现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 =_ blanktitle ='get_the_title($ post_object-> ID)。' ='。get_permalink($ post_object-> ID)。'>< span class =thumb>'。get_the_post_thumbnail($ post_object-> ID,'minimum')'< / span> < span class =thumb-title>< h6>'。get_the_title($ post_object-> ID)。< / h6>< / span>< / a>< '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 =_ blanktitle ='get_the_title($ post_object-> ID)。' ='。get_permalink($ post_object-> ID)。'>< span class =thumb>'。get_the_post_thumbnail($ post_object-> ID,'minimum')'< / span> < span class =thumb-title>< h6>'。get_the_title($ post_object-> ID)。< / h6>< / span>< / a>< '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 =_ blanktitle ='get_the_title($ post_object-> ID)。' ='。get_permalink($ post_object-> ID)。'>< span class =thumb>'。get_the_post_thumbnail($ post_object-> ID,'minimum')'< / span> < span class =thumb-title>< h6>'。get_the_title($ post_object-> ID)。< / h6>< / span>< / a>< 'sg_get_the_excerpt()。< / span>< / li>');
endforeach;
echo'< / ul>< / div>';
}


解决方案

$ plit array_slice 转换为 $ split !打开错误报告总是有用的,这有助于出现这样的错误: error_reporting(E_ALL)



您可能需要更改 $ split 变量,例如通过使用 ceil(),编辑:查看AndVla answer


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

解决方案

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

这篇关于PHP:计数数组中的项目,将总数除以2,创建两个UL列表,其中包含来自数组的包含项目的元素数量相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆