function selection_sort($arr) { $time_start = microtime(true); for ($x = 0; $x < count($arr); $x++) { $min = $arr[$x]; $max = $arr[$x]; for ($i = $x; $i < count($arr); $i++) { if ($arr[$i] < $min) { $min = $arr[$i]; $arr = array_swap($arr, $i, $x); } else { if ($arr[$i] > $max) { $max = $arr[$i]; $arr = array_swap($arr, count($arr) - 1, $i); } } } } var_dump($arr); $time_stop = microtime(true); echo $time_stop - $time_start; }
function my_custom_sizes($sizes) { /* NOTE: the $sizes array here is simply an associative array. It doesn't provide actual dimensions. We are hardcoding that Mugshot goes second now (and thumbnail first) ... a more robust solution could leverage something like https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes */ $newArray = array('mugshot' => 'Mugshot'); foreach ($sizes as $key => $value) { $newArray[$key] = $value; } $reorderedSizes = array_swap("mugshot", "thumbnail", $newArray); return $reorderedSizes; }
$correct_text_parts = explode("\t", $question['correct_text']); if (isset($answer_parts[$outer - 1])) { $answer_subparts = explode('$', $answer_parts[$outer - 1]); $csv .= ',"'; for ($k = 0; $k < count($correct_subparts); $k++) { if ($k > 0) { $csv .= '","'; } $diff = count($correct_subparts) - count($answer_subparts); if ($diff > 0) { $answer_subparts = array_pad($answer_subparts, -1 * ($diff + count($answer_subparts)), '-1'); } if (count($correct_subparts) > 1) { $corr_index = array_search($correct_subparts[$k], $answer_subparts); if ($corr_index !== false and $corr_index > $k) { $answer_subparts = array_swap($answer_subparts, $k, $corr_index); } } if ($answer_subparts[$k] != -1) { $subpart = $answer_subparts[$k]; if ($mode == 'numeric') { $csv .= $answer_subparts[$k]; } else { if (isset($correct_text_parts[$subpart])) { $csv .= $correct_text_parts[$subpart]; } } } if ($is_random) { if ($mode == 'numeric') { $csv .= '","' . $correct_subparts[$k];
/** * Change array element $num and [0] position * * @param array $arr (reference) * @param int $num * * @return void * * @author Kondratenko Alexander (Xander) */ function array_swap(&$arr, $num) { $tmp = $arr[0]; $arr[0] = $arr[$num]; $arr[$num] = $tmp; } // test data $arr = [4, 5, 8, 9, 1, 7, 2]; $i = 1; echo 'Inital ' . print_r($arr, TRUE) . "\n\r"; echo "Starting magic...Bang!\n\r\n\r"; while ($i < count($arr)) { foreach ($arr as $key => $_arr) { if ($_arr > $arr[0] && $key < count($arr) - $i + 1) { array_swap($arr, $key); } } array_swap($arr, count($arr) - $i); $i++; } echo 'Result ' . print_r($arr, TRUE) . "\n\r";