I have array of objects. Objects should be randomly shuffled for users and every user should have similar amount of objects. Every object have foreign key. If some objects have the same foreign key they should go to the same user. Does somebady know some good algorithm that would help me?
INPUT:
$users[] = {101,222,777}
$objects[] = [{
"counter" => 1,
"foreign_id" => 100001,
"some_data" => 'some_data',
'user' => null
},
{
"counter" => 2,
"foreign_id" => 100002,
"some_data" => 'some_data2',
'user' => null
},
{
"counter" => 3,
"foreign_id" => 100002,
"some_data" => 'some_data3',
'user' => null
},
{
"counter" => 4,
"foreign_id" => 100003,
"some_data" => 'some_data4',
'user' => null
},
{
"counter" => 5,
"foreign_id" => 100004,
"some_data" => 'some_data5',
'user' => null
}
];
OUTPUT
$objects[] = [{
"counter" => 1,
"foreign_id" => 100001,
"some_data" => 'some_data',
'user' => 101
},
{
"counter" => 2,
"foreign_id" => 100002,
"some_data" => 'some_data2',
'user' => 222
},
{
"counter" => 3,
"foreign_id" => 100002,
"some_data" => 'some_data3',
'user' => 222
},
{
"counter" => 4,
"foreign_id" => 100003,
"some_data" => 'some_data4',
'user' => 777
},
{
"counter" => 5,
"foreign_id" => 100004,
"some_data" => 'some_data5',
'user' => 101
}
];
php 5.4
Aucun commentaire:
Enregistrer un commentaire