Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffle inside array php

how to shuffle arrays in array ? I tried lots of way but I can't achieve that. I think it's very simple but I'm stuck on that.

Array
(
    [2] => Array
        (
            [0] => 12011190
            [1] => 12011158
            [2] => 12011583
            [3] => 12012107
            [4] => 12011222
            [5] => 12010638
            [6] => 12013836
            [7] => 12012232
            [8] => 12011256
            [9] => 12010007
            [10] => 12012531
            [11] => 12012182
            [12] => 12013253
        )

    [6] => Array
        (
            [0] => 12011565
            [1] => 12010020
            [2] => 12011352
            [3] => 12014366
            [4] => 12011879
            [5] => 12011449
        )
)

I want to shuffle within arrays. I hope explain...

like image 204
Yasin Yörük Avatar asked Nov 20 '25 01:11

Yasin Yörük


1 Answers

As far as I know, you can do it like this (assuming you want to shuffle every sub-array independently):

foreach($array AS &$element) {
    shuffle($element);
}

Or maybe like this:

array_walk($array, function(&$value, $key) {
    shuffle($value);
});
like image 189
Louis Huppenbauer Avatar answered Nov 21 '25 14:11

Louis Huppenbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!