How to filter an associative array comparing keys with values in an indexed array?
With array_intersect_key and array_flip: var_dump(array_intersect_key($my_array, array_flip($allowed))); array(1) { [“foo”]=> int(1) }
With array_intersect_key and array_flip: var_dump(array_intersect_key($my_array, array_flip($allowed))); array(1) { [“foo”]=> int(1) }
The following tests (compiled in Release mode, executed on a Mac Pro) indicate that filteredArrayUsingPredicate is slower than indexesOfObjectsPassingTest if you use a “textual” predicate, but faster if you use block-based predicate. The fasted method in my test was a simple (fast-enumeration) loop that adds all matching objects to a mutable array. Results for filtering … Read more
Interesting problem. There are several things going on here. No doubt this could be solved in less than half a page of Haskell or Lisp, but this is Java, so here we go…. One issue is that we have a variable number of filters, whereas most of the examples that have been shown illustrate fixed … Read more
Convert arrays to a format, where array index is the sight_id: $b1 =array(); foreach($a1 as $x) $b1[$x[‘sight_id’]] = $x[‘location’]; $b2 =array(); foreach($a2 as $x) $b2[$x[‘sight_id’]] = $x[‘location’]; Calculate the differences and intersection: $c_intersect = array_intersect_key($b1,$b2); $c_1 = array_diff_key($b1,$b2); $c_2 = array_diff_key($b2,$b1); Convert arrays back to your format: $intersect_array = array(); foreach($c_intersect as $i=>$v) $intersect_array[] = … Read more
To ignore case and accents (diacritics) you can first define an extension method like this: public static string RemoveDiacritics(this String s) { String normalizedString = s.Normalize(NormalizationForm.FormD); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < normalizedString.Length; i++) { Char c = normalizedString[i]; if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark) stringBuilder.Append(c); } return stringBuilder.ToString(); } (Modified … Read more
The Levenshtein distance is the algorithm I would recommend. It calculates the minimum number of operations you must do to change 1 string into another. The fewer changes means the strings are more similar…
You can get the filtered range dimensions from getFilter().getRange(). This will copy all the filtered range: function copycolA() { var sourceSheet = SpreadsheetApp.getActive().getSheetByName(‘Current’); var targetSheet = SpreadsheetApp.getActive().getSheetByName(‘A’); var sourceRange = sourceSheet.getFilter().getRange(); sourceRange.copyTo( targetSheet.getRange(‘A1’), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false); } To read: Filter#Range Related answer Note that .getValues() or other script operations will NOT get the filtered only, but … Read more
Try using array_map() to apply the filter to every array in $array: $array = array_map(‘array_filter’, $array); $array = array_filter($array); Demo: http://codepad.org/xfXEeApj
With array_intersect_key and array_flip: var_dump(array_intersect_key($my_array, array_flip($allowed))); array(1) { [“foo”]=> int(1) }
Functional approach: $array = array_filter($array, function($key) { return strpos($key, ‘foo-‘) === 0; }, ARRAY_FILTER_USE_KEY); Procedural approach: $only_foo = array(); foreach ($array as $key => $value) { if (strpos($key, ‘foo-‘) === 0) { $only_foo[$key] = $value; } } Procedural approach using objects: $i = new ArrayIterator($array); $only_foo = array(); while ($i->valid()) { if (strpos($i->key(), ‘foo-‘) === … Read more