mardi 23 février 2021

PHP: Equally random shuffle records for users [closed]

I'm looking for algorithm that could randomly shuffle stuff from 2 arrays. One array is array of objects. Objects should be randomly shuffled for users (2nd arr) and every user should have similar amount of objects.

ex: Let's say I have: 5 objects

{
    "id" => 1,                                                              
    "foreign_key" => 100001,  
    "some_data" => 'some_data',  
    'user' => null  
},

3 users

$users[] = {101,222,777}

INPUT:

$objects[] = [
    {
        "id" => 1,                                                              
        "foreign_key" => 100001,  
        "some_data" => 'some_data',  
        'user' => null  
    },
    {  
        "id" => 2,  
        "foreign_key" => 100002,  
        "some_data" => 'some_data2',  
        'user' => null  
    },
    {  
        "id" => 3,  
        "foreign_key" => 100002,  
        "some_data" => 'some_data3',  
        'user' => null  
    },
    {  
        "id" => 4,  
        "foreign_key" => 100003,  
        "some_data" => 'some_data4',  
        'user' => null  
    },
    {
        "id" => 5,  
        "foreign_key" => 100004,  
        "some_data" => 'some_data5',  
        'user' => null  
    }           
];  

Every object have foreign key. If some objects have the same foreign key they should go to the same user.

OUTPUT:

$objects[] = [
    {
        "id" => 1,                                                              
        "foreign_key" => 100001,  
        "some_data" => 'some_data',  
        'user' => 101  
    },  
    {  
        "id" => 2,  
        "foreign_key" => 100002,  
        "some_data" => 'some_data2',  
        'user' => 222  
    },
    {  
        "id" => 3,  
        "foreign_key" => 100002,  
        "some_data" => 'some_data3',  
        'user' => 222 
    },
    {  
        "id" => 4,  
        "foreign_key" => 100003,  
        "some_data" => 'some_data4',  
        'user' => 777  
    },
    {  
        "id" => 5,  
        "foreign_key" => 100004,  
        "some_data" => 'some_data5',  
        'user' => 101  
    }          
];  

Could sb help me?

php 5.4




Aucun commentaire:

Enregistrer un commentaire