Случайный 10 id по полу в php

Мой jSON:

{ 
"friends": {
"data": [
{ "id": "1000002470615", "gender": "female" },
{ "id": "1000005198891", "gender": "female" },
{ "id": "1000007859390", "gender": "female" },
{ "id": "1000008308250", "gender": "female" },
{ "id": "1000009416380", "gender": "male" },
{ "id": "1000010894609", "gender": "female" },
{ "id": "1000010991284", "gender": "male" },
{ "id": "1000011095914", "gender": "female" },
{ "id": "1000014648465", "gender": "female" },
{ "id": "1000017041889", "gender": "female" },
{ "id": "1000017519651", "gender": "female" },
{ "id": "1000019029610", "gender": "female" },
{ "id": "1000019497511", "gender": "female" },
{ "id": "1000019681751", "gender": "female" },
{ "id": "1000020582845", "gender": "male" },
{ "id": "1000021389766", "gender": "male" },
{ "id": "1000022247272", "gender": "female" },
{ "id": "1000025425217", "gender": "female" },
{ "id": "1000026359294", "gender": "female" },
{ "id": "1000031258301", "gender": "male" },
{ "id": "1000031499108", "gender": "female" },
{ "id": "1000032349067", "gender": "male" },
{ "id": "1000032428345", "gender": "female" },
{ "id": "1000033101313", "gender": "male" },
{ "id": "1000033288824", "gender": "female" },
{ "id": "1000033525527", "gender": "female" },
{ "id": "1000034062687", "gender": "male" },
{ "id": "1000035232214", "gender": "female" },
{ "id": "1000035804034", "gender": "female" },
{ "id": "1000040066742", "gender": "male" },
{ "id": "1000041457943", "gender": "female" },
{ "id": "1000042302703", "gender": "female" },
{ "id": "1000043758095", "gender": "female" },
{ "id": "1000045378342", "gender": "male" },
{ "id": "1000048531666", "gender": "female" },
{ "id": "1000049731704", "gender": "female" },
{ "id": "1000050719295", "gender": "female" },
{ "id": "1000050736082", "gender": "male" },
{ "id": "1000051733891", "gender": "female" },
{ "id": "1000052976810", "gender": "male" },
{ "id": "1000053432719", "gender": "male" },
{ "id": "1000054595524", "gender": "male" },
{ "id": "1000055567339", "gender": "female" },
{ "id": "1000056180174", "gender": "female" },
{ "id": "1000056737207", "gender": "male" },
{ "id": "1000058322123", "gender": "male" },
{ "id": "1000058592478", "gender": "female" },
{ "id": "1000059646724", "gender": "male" },
{ "id": "1000062848532", "gender": "female" },
{ "id": "1000062994936", "gender": "female" },
{ "id": "1000066441440", "gender": "male" } ],
}
}

Мне нужно случайным образом отображать 10 идентификаторов в зависимости от пола.

Как в примере ниже:

if ( $ps == 1 ) {
 echo 'Random 10 male gender IDs';
} else {
 echo 'Random 10 female gender IDs';
}
3 ответа

$gender = 'female';
$count = 10;
$myArray = json_decode($myJSONData);
$subset = array_filter(
 $myArray['friends']['data'],
 function($value) use ($gender) {
 return $value['gender'] == $gender;
 }
);
shuffle($subset);
$newArray = array_slice(
 $subset,
 0,
 $count
);


<!--?php

$array = json_decode('{ "friends": { "data": [ { "id": "1000002470615", "gender": "female" }, { "id": "1000005198891", "gender": "female" }, { "id": "1000007859390", "gender": "female" }, { "id": "1000008308250", "gender": "female" }, { "id": "1000009416380", "gender": "male" }, { "id": "1000010894609", "gender": "female" }, { "id": "1000010991284", "gender": "male" }, { "id": "1000011095914", "gender": "female" }, { "id": "1000014648465", "gender": "female" }, { "id": "1000017041889", "gender": "female" }, { "id": "1000017519651", "gender": "female" }, { "id": "1000019029610", "gender": "female" }, { "id": "1000019497511", "gender": "female" }, { "id": "1000019681751", "gender": "female" }, { "id": "1000020582845", "gender": "male" }, { "id": "1000021389766", "gender": "male" }, { "id": "1000022247272", "gender": "female" }, { "id": "1000025425217", "gender": "female" }, { "id": "1000026359294", "gender": "female" }, { "id": "1000031258301", "gender": "male" }, { "id": "1000031499108", "gender": "female" }, { "id": "1000032349067", "gender": "male" }, { "id": "1000032428345", "gender": "female" }, { "id": "1000033101313", "gender": "male" }, { "id": "1000033288824", "gender": "female" }, { "id": "1000033525527", "gender": "female" }, { "id": "1000034062687", "gender": "male" }, { "id": "1000035232214", "gender": "female" }, { "id": "1000035804034", "gender": "female" }, { "id": "1000040066742", "gender": "male" }, { "id": "1000041457943", "gender": "female" }, { "id": "1000042302703", "gender": "female" }, { "id": "1000043758095", "gender": "female" }, { "id": "1000045378342", "gender": "male" }, { "id": "1000048531666", "gender": "female" }, { "id": "1000049731704", "gender": "female" }, { "id": "1000050719295", "gender": "female" }, { "id": "1000050736082", "gender": "male" }, { "id": "1000051733891", "gender": "female" }, { "id": "1000052976810", "gender": "male" }, { "id": "1000053432719", "gender": "male" }, { "id": "1000054595524", "gender": "male" }, { "id": "1000055567339", "gender": "female" }, { "id": "1000056180174", "gender": "female" }, { "id": "1000056737207", "gender": "male" }, { "id": "1000058322123", "gender": "male" }, { "id": "1000058592478", "gender": "female" }, { "id": "1000059646724", "gender": "male" }, { "id": "1000062848532", "gender": "female" }, { "id": "1000062994936", "gender": "female" }, { "id": "1000066441440", "gender": "male" } ] } }', true);

$randommales = array();
$randomfemales = array();

$allmales = array();
$allfemales = array();

foreach($array['friends']['data'] as $key =--> $details) {
 if ($details['gender'] == 'male') {
 $allmales[] = $key;
 } else {
 $allfemales[] = $key;
 }
}

for($i = 0; $i < 10; $i++){
 $randommales[] = $array['friends']['data'][$allmales[rand(0, count($allmales))]];
 $randomfemales[] = $array['friends']['data'][$allfemales[rand(0, count($allfemales))]];
}

В этом случае будет 10 случайных мужчин и женщин в $ randommales и $ randomfemales.


Это похоже на результат API Facebook. ;-)

<!--?php

$my_sorted_data = array('male' =--> array(), 'female' => array());
foreach ($friends['data'] as $data) {
 $my_sorted_data[$data['gender']][] = $data['id'];
}

// Then if you want a mix of female... just shuffle it
shuffle($my_sorted_data['female']);
$my_result = array_slice($my_sorted_data['female'], 0, 10);

licensed under cc by-sa 3.0 with attribution.