$after = multiLevelShuffle($before); // run basic shuffles $after = shuffle2dArray($after); // run advanced shuffles $timer = microtime(true) - $timer; // calculate difference since start $timer = round($timer * 1000000, 0); // multiply by 1,000,000 and round $tableTimer = microtime(true); // show the before shuffling version echo '<div class="before"><div id="RF"><h2>Before</h2>'; display2dArray($before); echo '</div></div>'; // show the after shuffling version echo '<div class="after"><h2>After</h2>'; display2dArray($after); echo '</div>'; $tableTimer = round((microtime(true) - $tableTimer) * 1000000, 0); // calculate timer to rounded to nearest microsecond } ?> <!-- Debug to make sure I'm getting the right stuff back --> <dl class="brand"> <dt>Filename</dt> <dd><code><?php echo $store['name']; ?> </code></dd> <dt>File location</dt> <dd><code><?php
function shuffle2dArray($array, $debug = false) { $notShuffle = array('' => true, '0' => true, 'off' => true, 'no' => true); if ($debug) { $startCopy = $array; } $showShuffles = false; $padding = array(); $firstRow = current($array); while (!is_array($firstRow)) { $padding[] = array_shift($array); $firstRow = current($array); } $headers = array_keys($firstRow); $prevColumns = array(); // go through each of the headers, and see if it contains shuffle instructions foreach ($headers as $header) { // if the column doesn't explicitly say which columns to target, // then we will just use the previous columns as the target $prevColumns[$header] = true; if (stripos($header, 'advshuffle') !== false) { $showShuffles = true; $shuffleInfo = explode(';', $header); // the type of shuffle (simple, block, or list), should be before the first semicolon $type = array_shift($shuffleInfo); $target = findSetting($shuffleInfo, 'target'); if (is_bool($target)) { // if false, "target" wasn't found. // if true, they had the "target" keyword, but no columns // either way, just shuffle the columns up to this point $targets = $prevColumns; } else { // targets can be specified as ranges, like "Target: 1,2,5-8,10" $target = explode(',', $target); $targets = array(); foreach ($target as $targ) { $these = explode('-', $targ); $first = array_shift($these); $first = findIndex($headers, $first, $header); if ($these !== array()) { // if they did specify a range (e.g. 1-3), $these won't be empty after array_shift // if so, get all the columns in this range, and add them to $targets $last = array_pop($these); $last = findIndex($headers, $last, $header); $targRange = range($first, $last); foreach ($targRange as $t) { $targets[$headers[$t]] = true; } } else { $targets[$headers[$first]] = true; } } } // targets should include itself, so that the shuffle column will reflect its own changes $targets[$header] = true; $within = findSetting($shuffleInfo, 'within'); if (is_bool($within)) { // if they didn't specify a within column, just make a temp array // and pretend everything is in the same within category $within = array(); foreach ($array as $i => $row) { $within[$i] = '1'; } } else { $within = findIndex($headers, $within, $header); $withinCol = $headers[$within]; // add the within to the targets, so that if block shuffle // affects the order of the non-shuffled rows, we can still see that // also, we can display the within during debugging $targets[$withinCol] = true; $within = array(); foreach ($array as $i => $row) { $within[$i] = $row[$withinCol]; } } // we just want to grab the target columns from each of the rows $shuffled = array(); foreach ($array as $i => $row) { foreach ($targets as $t => $unused) { $shuffled[$i][$t] = $row[$t]; } } if ($debug) { echo '<div style="white-space: nowrap">'; echo 'Shuffle Column: ' . $header . '<br>'; echo '<div style="display: inline-block">'; echo 'Before shuffle:<br>'; display2dArray($shuffled); echo '</div>'; } if (stripos($type, 'block') !== false) { superBlockShuffle($shuffled, $header, $within, $notShuffle); } elseif (stripos($type, 'list') !== false) { listShuffle($shuffled, $header, $within, $notShuffle); } elseif (stripos($type, 'side') !== false) { sideShuffle($shuffled, $header, $within, $notShuffle); } else { simpleShuffle($shuffled, $header, $within, $notShuffle); } if ($debug) { echo '<div style="display: inline-block">'; echo 'After shuffle:<br>'; display2dArray($shuffled); echo '</div>'; echo '</div>'; } // put the shuffled result back in the array foreach ($shuffled as $i => $row) { foreach ($row as $t => $value) { $array[$i][$t] = $value; } } } } foreach (array_reverse($padding) as $row) { array_unshift($array, $row); } if ($debug and $showShuffles) { echo '<div style="white-space: nowrap">'; echo '<div style="display: inline-block">'; echo 'Before all advanced shuffles:<br>'; display2dArray($startCopy); echo '</div>'; echo '<div style="display: inline-block">'; echo 'After advanced shuffles:<br>'; display2dArray($array); echo '</div>'; echo '</div>'; } return $array; }