lundi 10 décembre 2018

Ways of getting 20% of elements in array - PHP

I have an array of n elements and I need to get random 50% of those elements into another array. Is there any function which could do such a trick?

Currently what I can think of is this:

foreach ($orders as $order) {
    if (rand(1, 100) > 80) {
        echo('20%');
    } else {
        echo('80%');
    }
}

Is there a more optimal way?




Aucun commentaire:

Enregistrer un commentaire