@@ -28,9 +28,9 @@ function array_rand_value(array $array, $numReq = 1)
2828/**
2929 * Get a random value from an array, with the ability to skew the results.
3030 * Example: array_rand_weighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
31- *
31+ *
3232 * @param array $array
33- *
33+ *
3434 * @return mixed
3535 */
3636function array_rand_weighted (array $ array )
@@ -65,7 +65,7 @@ function values_in_array($needles, array $haystack)
6565
6666/**
6767 * Determine if all given needles are present in the haystack as array keys.
68- *
68+ *
6969 * @param array|string $needles
7070 * @param array $haystack
7171 *
@@ -77,5 +77,30 @@ function array_keys_exist($needles, array $haystack)
7777 return array_key_exists ($ needles , $ haystack );
7878 }
7979
80- return values_in_array ($ needles , array_keys ($ haystack ));
80+ return values_in_array ($ needles , array_keys ($ haystack ));
81+ }
82+
83+ /**
84+ * Returns an array with two elements.
85+ *
86+ * Iterates over each value in the array passing them to the callback function.
87+ * If the callback function returns true, the current value from array is returned in the first
88+ * element of result array. If not, it is return in the second element of result array.
89+ *
90+ * Array keys are preserved.
91+ *
92+ * @param array $array
93+ * @param callable $callback
94+ *
95+ * @return array
96+ */
97+ function array_split_filter (array $ array , callable $ callback )
98+ {
99+ $ passesFilter = array_filter ($ array , $ callback );
100+
101+ $ negatedCallback = function ($ item ) use ($ callback ) { return ! $ callback ($ item ); };
102+
103+ $ doesNotPassFilter = array_filter ($ array , $ negatedCallback );
104+
105+ return [$ passesFilter , $ doesNotPassFilter ];
81106}
0 commit comments