Skip to content

Instantly share code, notes, and snippets.

@Dare-NZ
Last active December 17, 2015 13:19
Show Gist options
  • Save Dare-NZ/5616246 to your computer and use it in GitHub Desktop.
Save Dare-NZ/5616246 to your computer and use it in GitHub Desktop.
Does what it says on the tin, merges three arrays to a looping pattern
<?php
$pattern = array(0,0,0,1,2,2,2);
$news_array = array('news1','news2','news3','news4','news5','news6','news7','news8');
$video_array = array('video1','video2','video3','video4','video5','video6','video7','video8');
$photo_array = array('photo1','photo2','photo3','photo4','photo5','photo6','photo7','photo8','photo9','photo10','photo11','photo12','photo13');
$asset_array = array($news_array, $video_array, $photo_array);
$type_count = sizeof($asset_array);
$pattern_count = sizeof($pattern) - 1;
$total_length = array_total_size($asset_array);
$new_array = array();
for($i=0;$i<9999;$i++) {
$pattern_index = $i % $pattern_count;
$select_index = $pattern[$pattern_index];
echo $select_index . '<br />';
if(!empty($asset_array[$select_index])) {
$new_item = array_shift($asset_array[$select_index]);
if(isset($new_item)) $new_array[] = $new_item;
}
if(array_total_size($asset_array) == 0) break;
}
foreach($new_array as $item) {
echo $item . '<br />';
}
function array_total_size($arr) {
$count = 0;
foreach($arr as $subarr) {
$count = $count + sizeof($subarr);
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment