It's possible to shuffle array once a week without storing anything. I've found the solution at StackOverflow.
It's based on initializing random number generator which is used by shuffle()
with a seed that's constant for each week. date('W')
is a week number in a year which fits the purpose well. After shuffling is done RNG is put back to normal by calling srand()
with no argument i.e. initilizing it with random seed.
srand(date('W'));
shuffle($array);
srand();
Check whether haystack ends with needle.
function ends_with($haystack, $needle) {
if ($needle === '') {
return true;
}
return strtolower(substr($haystack, -strlen($needle))) === $needle;
}
Check whether haystack starts with needle.
function starts_with($haystack, $needle) {
return $needle === '' || stripos($haystack, $needle) === 0;
}
Despite it's quite common requirement to assert that elements in two arrays are the same while order doesn't matter, it's not that obvious on how to do it in PHPUnit.
$array = ['b', 'a'];
$this->assertEquals(['a', 'b'], $array, '', 0.0, 10, true);
Since PHP 5.4 it's possible to get script execution time without prior recording of script initial timestamp.
echo microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
Run the following command to reveal your IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'
In order to delete item from fragment cache in Yii 2.0 you need to form a key in a special way.
function deleteFragmentCacheByKey($key)
{
return Yii::$app->cache->delete(['yii\widgets\FragmentCache', $key]);
}
A short and reliable way to detect if the script is run in Windows environment.
if (DIRECTORY_SEPARATOR === '\\') {
// ...
}
There are cases when you need to make sure keys specified are in the beginning of an array in the exact order they are specified.
$data = [
'orange' => 'orange',
'apple' => 'tasty',
'carpet' => 'old',
'car' => 'fast',
];
$result = orderByKeys($data, ['car', 'carpet']);
Would result in:
$data = [
'car' => 'fast',
'carpet' => 'old',
'orange' => 'orange',
'apple' => 'tasty',
];
function orderByKeys(array $array, array $keys)
{
foreach ($keys as $k => $v) {
if (!array_key_exists($v, $array)) {
unset($keys[$k]);
}
}
return array_replace(array_flip($keys), $array);
}