File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -1288,6 +1288,39 @@ function compare_json($first_object, $second_object)
12881288 }
12891289}
12901290
1291+ if ( ! function_exists ('to_generator ' ))
1292+ {
1293+ /**
1294+ * Convert an array or object
1295+ * to a generator
1296+ *
1297+ * @param mixed $data
1298+ * @param integer $threshold
1299+ * @return \Generator|array|object
1300+ */
1301+ function to_generator ($ data , $ threshold = 1000 )
1302+ {
1303+ $ count = 0 ;
1304+
1305+ if (is_array ($ data )) {
1306+ $ count = count ($ data );
1307+ }
1308+
1309+ if (is_object ($ data )) {
1310+ $ count = count (get_object_vars ($ data ));
1311+ }
1312+
1313+ if ($ count > $ threshold ) {
1314+ foreach ($ data as $ key => $ value ) {
1315+ yield $ key => $ value ;
1316+ }
1317+ } else {
1318+ return $ data ;
1319+ }
1320+
1321+ }
1322+ }
1323+
12911324/* ------------------------------- Date | Time | Format Functions ---------------------------------*/
12921325
12931326
You can’t perform that action at this time.
0 commit comments