File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed
Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -221,6 +221,20 @@ foreach ($qb->getResults() as $element){
221221}
222222```
223223
224+ ## Shuffled Results
225+
226+ You can shuffle query results by using ` getShuffledResults ` method:
227+
228+ ``` php
229+ use ArrayQuery\QueryBuilder;
230+
231+ $qb = QueryBuilder::create($array);
232+
233+ foreach ($qb->getShuffledResults() as $element){
234+ // ...
235+ }
236+ ```
237+
224238## More examples
225239
226240Please refer to [ QueryBuilderTest] ( https://github.com/mauretto78/array-query/blob/master/tests/QueryBuilderTest.php ) for more examples.
Original file line number Diff line number Diff line change @@ -225,6 +225,22 @@ public function getLastResult()
225225 return $ this ->getResults ()[$ count -1 ] ?: [];
226226 }
227227
228+ /**
229+ * @return array
230+ */
231+ public function getShuffledResults ()
232+ {
233+ $ shuffledArray = [];
234+ $ keys = array_keys ($ this ->getResults ());
235+ shuffle ($ keys );
236+
237+ foreach ($ keys as $ key ) {
238+ $ shuffledArray [$ key ] = $ this ->getResults ()[$ key ];
239+ }
240+
241+ return $ shuffledArray ;
242+ }
243+
228244 /**
229245 * @param array $array
230246 *
Original file line number Diff line number Diff line change @@ -617,4 +617,16 @@ public function it_should_get_results_from_a_query_with_aliases()
617617 $ this ->assertArrayHasKey ('add ' , $ qb ->getResults ()[1 ]);
618618 }
619619 }
620+
621+ /**
622+ * @test
623+ */
624+ public function it_should_get_shuffled_results_from_a_query ()
625+ {
626+ foreach ($ this ->usersArrays as $ array ) {
627+ $ qb = QueryBuilder::create ($ array );
628+
629+ $ this ->assertCount (10 , $ qb ->getShuffledResults ());
630+ }
631+ }
620632}
You can’t perform that action at this time.
0 commit comments