{"id":126,"date":"2011-08-31T14:49:57","date_gmt":"2011-08-31T19:49:57","guid":{"rendered":"http:\/\/www.marcblase.com\/blog\/?p=126"},"modified":"2014-11-21T13:06:56","modified_gmt":"2014-11-21T19:06:56","slug":"chunk-arrays-with-php","status":"publish","type":"post","link":"https:\/\/ma.rcbla.se\/blog\/2011\/08\/chunk-arrays-with-php\/","title":{"rendered":"Chunk arrays with php"},"content":{"rendered":"<p>For a while now I have been using a bit of code for reading the contents of a directory, pushing the found items into an array, then looping over the array with a foreach to create markup for a slideshow, header images, backgrounds, etc. A current project I am working on needs this same functionality but with a twist. I still need to loop over the directory but I need to group the contents into groups or chunks. Enter, <a href=\"http:\/\/php.net\/manual\/en\/function.array-chunk.php\" target=\"_blank\">PHP&#8217;s array_chunk()<\/a>. This does exactly what I need, but to get the array&#8217;s contents neatly on screen I am nesting a set of foreach loops. One for the first subarray, then the other for that subarray&#8217;s contents. It goes something like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ Initialize the array to hold images\r\n$thelist = array();\r\n\/\/ Open directory and add files into array\r\nif ($handle = opendir($dir_path)) {\r\n   \/\/ Loop over the directory.\r\n   while (false !== ($file = readdir($handle))) { \r\n\t\/\/ Don&#039;t include the &quot;.&quot;  and \r\n        \/\/ &quot;..&quot; files. As well as the \r\n        \/\/ DS_Store files left by the Mac OS\r\n\tif ( $file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot; &amp;&amp; $file != &quot;.DS_Store&quot; ) {\r\n\t    array_push($thelist,$file);\r\n\t}\r\n   }\r\n   closedir($handle);\r\n}\r\n\r\nnatsort($thelist); \/\/ Put the array in alphabetical order\r\n\/\/shuffle($thelist); \/\/ Put the array in random order\r\n\r\n\/\/ Split array into chunks\r\n$subarray = (array_chunk($thelist, 5));\r\n\r\n\/\/ Layout the contents\r\necho &quot;&lt;ul&gt;\\n&quot;;\r\n\r\nforeach ($subarray as $arraygroup) {\r\n\r\n\techo &quot;&lt;li&gt;&quot;;\r\n\t\r\n\tforeach ($arraygroup as $arrayitem) {\r\n             \/\/ Do pretty markup here\r\n             echo &quot;&lt;p&gt;Item: $arrayitem&lt;\/p&gt;&quot;;\r\n        } \/\/ END nest\r\n\t\r\n        echo &quot;&lt;\/li&gt;&quot;;\r\n\t\r\n} \/\/ END main\r\n\r\necho &quot;&lt;\/ul&gt;\\n&quot;;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For a while now I have been using a bit of code for reading the contents of a directory, pushing the found items into an array, then looping over the array with a foreach to create markup for a slideshow, header images, backgrounds, etc. A current project I am working on needs this same functionality [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,3],"tags":[26,16],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-coding","category-discoveries","tag-arrays","tag-php"],"_links":{"self":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":10,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}