Quantcast
Channel: User Ja͢ck - Stack Overflow
Browsing all 44 articles
Browse latest View live

Answer by Ja͢ck for PHP Overriding methods rules

The rule is that a method signature must be compatible with the method it overrides. Let's look at the two methods in your hierarchy:protected function playing($game = 'ball');public function...

View Article


Answer by Ja͢ck for ruby: how to find non-unique elements in array and print...

From Ruby 2.7, you can utilise Enumerable#tally and numbered block arguments:a = ["a", "d", "c", "b", "b", "c", "c"]puts a.tally.filter { _2 > 1 }.sort_by { -_2 }.map &:firstHere,...

View Article


Answer by Ja͢ck for Iterate a PHP array from a specific key

If this pull request makes it through, you will be able to do this quite easily:if (seek($array, 'yy', SEEK_KEY)) { while ($data = each($array)) { // Do stuff }}

View Article

Answer by Ja͢ck for Efficiently counting the number of lines of a text file....

Using a loop of fgets() calls is fine solution and the most straightforward to write, however:even though internally the file is read using a buffer of 8192 bytes, your code still has to call that...

View Article

Answer by Ja͢ck for Trying to test my randomiser in rspec by passing specific...

Your weather_randomiser does too many things:It generates a random numberIt maps a random number to a string valueIt updates an instance variableTo make things more testable, you could split those out,...

View Article


Answer by Ja͢ck for Sum values in one column of a 2d array

You need to couple it with array_map() to select the f_count column first:array_sum(array_map(function($item) { return $item['f_count']; }, $arr));Nowadays you can replace the inner function with...

View Article

Answer by Ja͢ck for Filter a flat, associative array by the keys in another...

To get the elements that exist in $arr2 which also exist in $arr1 (i.e. drop elements of $arr2 that don't exist in $arr1), you can intersect based on the key like this:array_intersect_key($arr2,...

View Article

Answer by Ja͢ck for Can you include raw JSON in Guzzle POST Body?

You can send a regular array as JSON via the 'json' request option; this will also automatically set the right headers:$headers = ['NETOAPI_KEY' => env('NETO_API_KEY'),'Accept' =>...

View Article


Answer by Ja͢ck for javascript: stop setInterval after element removal

Have you tried storing the intervalId on the #element itself using $.data?$('#element').mouseover(function() { var $this = $(this); $.post('ajax/ajax.php', function(data) { $('<div...

View Article


Answer by Ja͢ck for Javascript passing arrays to functions by value, leaving...

Inside your function there's this:funcArray = new Array();funcArray = someArray;This won't actually copy someArray but instead reference it, which is why the original array is modified.You can use...

View Article

Answer by Ja͢ck for PHP Regex: match character set OR end of string

To match a string that starts with a slash, followed by six alphanumerics and is then followed by either the end-of-string or something that's not...

View Article

Answer by Ja͢ck for Element ID from document.getElementsByClassName

Using in is not how you should iterate over an array of elements. You should use the .length property and use numeric indexing:for (var i = 0; i < divstopop.length; ++i) { // Get id property from...

View Article

Answer by Ja͢ck for Ruby - functional approach to filtering with index

After many years, Ruby 2.7 ships with Enumerable#filter_map() to make this simpler:stuff.filter_map.with_index { |elem, index| index if elem == '' }

View Article


Answer by Ja͢ck for Search for part of an key in array - PHP

You can use a negative filter (with strpos) instead:foreach ($array as $key => $value) { if (strpos($key, 'array_') !== 0) { unset($array[$key]); }}DemoNote that it modifies the array...

View Article

Answer by Ja͢ck for join enumerable to produce string in Ruby

The code within your question is already as optimal as it gets, but it's possible to remove the condition inside your block:e = ('a'..'z').to_enumjoined_string = e.reduce { |acc, str| acc << '...

View Article


Answer by Ja͢ck for how to generate unique id in php based on current data...

With PHP 7 you can use this to generate a sufficiently random value based on the current timestamp:$s = time() . bin2hex(random_bytes(10));Do note that after 265 years (starting with 11 digits in 2286)...

View Article

Answer by Ja͢ck for creating a random number using MYSQL

This should give what you want:FLOOR(RAND() * 401) + 100Generically, FLOOR(RAND() * (<max> - <min> + 1)) +<min> generates a number between <min> and <max>...

View Article


Answer by Ja͢ck for PHP function to generate v4 UUID

To construct a UUIDv4 you can generate 128 bits worth of random data, patch a few fields to make the data comply with the standard, and then format it as hexadecimal groups.According to RFC 4122 -...

View Article

Answer by Ja͢ck for Fastest way to handle undefined array key

UpdateSince PHP 7 you can accomplish this with the null coalescing operator (keeping in mind that it will bypass existing array element with null value, the same way isset would):return $table[$key] ??...

View Article

Comment by Ja͢ck on Disable loading of nested locale (I18n) folders in Rails 7

For those interested, you can find the rails change here: github.com/rails/rails/pull/41872

View Article
Browsing all 44 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>