PHP: Count items in array, split total by two, create two UL lists with equal number of elements containing items from array

五迷三道 提交于 2019-12-25 02:16:11

问题


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

回答1:


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:


Think you can solve the problem like that:

$split = ($items+1) / 2;

or

$split = ceil($items / 2);



回答3:


tim is right, however you also probably want the firsthalf to be the first half of the array, not vise-versa like you have it now. This is because slice arguments are: $output = array_slice($input, $offset, $length);. So you'll want to set your variables like this $firsthalf = array_slice($panels, 0, $split); $secondhalf = array_slice($panels, $split, $items);

Cheers



来源:https://stackoverflow.com/questions/8573717/php-count-items-in-array-split-total-by-two-create-two-ul-lists-with-equal-nu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!