Beispiel #1
0
/**
 * Removes duplicates from a list by keeping the first occurence of each group
 * of items which are equivalent according to the given `$areEqual` callable.
 * ```php
 * uniqueBy(eq(), [1, '2', '1', 3, '3', 2, 2]); // [1, '2', 3]
 * ```
 *
 * @signature (a -> a -> Boolean) -> [a] -> [a]
 * @param  callable $areEqual
 * @param  array $list
 * @return array
 */
function uniqueBy()
{
    $uniqueBy = function ($areEqual, $list) {
        // make sure the compare function is curried
        $compare = function ($a) use($areEqual) {
            return function ($b) use($areEqual, $a) {
                return $areEqual($a, $b);
            };
        };
        return reduce(function ($result, $item) use($compare) {
            return null === findIndex($compare($item), $result) ? append($item, $result) : $result;
        }, [], $list);
    };
    return apply(curry($uniqueBy), func_get_args());
}
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;
}
Beispiel #3
0
<?php

//Linear Search : Tìm kiếm tuyến tính
function findIndex($input = array(), $output = '')
{
    for ($i = 0; $i < count($input); ++$i) {
        if ($input[$i] == $output) {
            return $i . '-----' . $input[$i];
        }
        return FALSE;
    }
}
$array = array('tung', 'trang');
$flag = findIndex($array, 'tung');
echo $flag;
function sort($array_1 = array())
{
    for ($i = 0; $i < count($array_1); ++$i) {
        $temp = $array_1[$i];
        for ($j = $i - 1; $j >= 0 && $array_1[$j] > $temp; --$j) {
            $array_1[$j + 1] = $array_1[$j];
        }
        $array_1[$j + 1] = $temp;
    }
}
$array_1 = array(1, 3, 4, 6, 8, 10, 9);
sort($array_1);