Last active
September 10, 2018 15:10
-
-
Save kcclemo/a440bdc09f11811e0740e6d42b5f7729 to your computer and use it in GitHub Desktop.
Break an array into incremental chunks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = partners.csv; | |
$rows = array_map('str_getcsv', file($file)); | |
$header = array_shift($rows); | |
$partners = []; | |
$slides = []; | |
foreach($rows as $row) { | |
$partners[] = array_combine($header, $row); | |
} | |
// Break the partners array into sub-arrays containing 16 partners per chunk. | |
$slides = array_chunk($partners, 16, true); | |
// Iterator for building the slider. | |
$i = 0; | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Chunking an array</title> | |
</head> | |
<body> | |
<div class="partner-gallery"> | |
<?php // Create a set of slides based on the chunks ?> | |
<?php foreach($slides as $partner) { ?> | |
<div class="slide"> | |
<ul> | |
<?php // Create sub-items inside each slide. ?> | |
<?php foreach($partner as $partner) { ?> | |
<li> | |
<?php // Echo out each partner's information ?> | |
<a href="<?= $partner['link'] ?>"> | |
<img <?= ($i === 0) ? 'src' : 'data-lazy' ?>="<?= $partner['logo'] ?>" alt="<?= $partner['brand'] ?>"> | |
</a> | |
</li> | |
<?php } ?> | |
</ul> | |
</div> | |
<?php $i++; ?> | |
<?php } ?> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple PHP script I created to build a slick.js carousel that contained a 4x4(16 total) grid of affiliate links per slide from a CSV file.